Clearing content of text file using php

2020-02-16 07:37发布

I have a filelist.txt file and I created a file called clear.php to clear the content of filelist.

I put a button in index.html to call clear.php to clear the file.

Can anyone help me out regarding what PHP code I should write in clear.php?

How to code a button to call clear.php and then return back to index.html showing the result that it has been cleared?

7条回答
叛逆
2楼-- · 2020-02-16 07:51
//create a file handler by opening the file
$myTextFileHandler = @fopen("filelist.txt","r+");

//truncate the file to zero
//or you could have used the write method and written nothing to it
@ftruncate($myTextFileHandler, 0);

//use location header to go back to index.html
header("Location:index.html");

I don't exactly know where u want to show the result.

查看更多
放荡不羁爱自由
3楼-- · 2020-02-16 07:58
file_put_contents("filelist.txt", "");

You can redirect by using the header() function to modify the Location header.

查看更多
地球回转人心会变
4楼-- · 2020-02-16 07:59
 $fp = fopen("$address",'w+');
 if(!$fp)
    echo 'not Open';
        //-----------------------------------
 while(!feof($fp))
     {
        fputs($fp,' ',999);                    
     } 

 fclose($fp);
查看更多
神经病院院长
5楼-- · 2020-02-16 08:02

This would truncate the file:

$fh = fopen( 'filelist.txt', 'w' );
fclose($fh);

In clear.php, redirect to the caller page by making use of $_SERVER['HTTP_REFERER'] value.

查看更多
戒情不戒烟
6楼-- · 2020-02-16 08:03

Use 'w' and not, 'a'.

if (!$handle = fopen($file, 'w'))
查看更多
Lonely孤独者°
7楼-- · 2020-02-16 08:04

Try fopen() http://www.php.net/manual/en/function.fopen.php

w as mode will truncate the file.

查看更多
登录 后发表回答