Possible Duplicate:
PHP: Is there a command that can delete the contents of a file without opening it?
How do you empty a .txt file on a server with a php command?
To delete a file in PHP, use the unlink function.
PHP File handling functions :copy() – used to copy a file. rename() – used to rename a file. unlink() – used to delete a file.
PHP | unlink() Function The unlink() function is an inbuilt function in PHP which is used to delete files. It is similar to UNIX unlink() function. The $filename is sent as a parameter that needs to be deleted and the function returns True on success and false on failure. Syntax: unlink( $filename, $context )
The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure.
$fh = fopen('filename.txt','w'); // Open and truncate the file
fclose($fh);
Or in one line and without storing the (temporary) file handle:
fclose(fopen('filename.txt','w'));
As others have stated, this creates the file in case it doesn't exist.
Write an empty string as the content of filename.txt
:
file_put_contents('filename.txt', '');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With