tags:

views:

517

answers:

5

I have a filelist.txt file

and i create clear.php file to clear the content of filelist

in index.html i put a button to call clear.php to clear

Can anyone help me out regarding:

What php code should i 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?

Thank You

+2  A: 

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

w as mode will truncate the file.

Fredrik
+4  A: 

try

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

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

Andy E
+3  A: 

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.

Alan Haggai Alavi
Your solution helped me, but i implemented Andy E's solution, well your solution is also 100% correct.Thank you for helping me out!!
Sunny Rockzzs
A: 

//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.

A: 

if (!$handle = fopen($file, 'w'))

use 'w' and not, 'a'

Martin Clavell