Clearing content of text file using php


Question

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?

1
50
2/17/2019 7:33:23 PM

Accepted Answer

file_put_contents("filelist.txt", "");

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

110
10/6/2015 1:40:07 PM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon