Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing content of text file using php

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?

like image 451
Subho Halder Avatar asked Jul 02 '09 10:07

Subho Halder


People also ask

How to clear txt file in PHP?

$fh = fopen( 'filelist. txt', 'w' ); fclose($fh); In clear. php, redirect to the caller page by making use of $_SERVER['HTTP_REFERER'] value.

How to empty file in PHP?

In PHP, we can delete any file using unlink() function. The unlink() function accepts one argument only: file name. It is similar to UNIX C unlink() function.


2 Answers

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

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

like image 143
Andy E Avatar answered Oct 02 '22 04:10

Andy E


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.

like image 41
Alan Haggai Alavi Avatar answered Oct 02 '22 04:10

Alan Haggai Alavi