Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emptying a file with php [duplicate]

Tags:

php

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?

like image 230
Chris Mccabe Avatar asked Feb 09 '11 11:02

Chris Mccabe


People also ask

How do you empty a file in PHP?

To delete a file in PHP, use the unlink function.

How do you copy & delete a files in PHP by using copy and unlink?

PHP File handling functions :copy() – used to copy a file. rename() – used to rename a file. unlink() – used to delete a file.

What is unlink function in PHP?

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 )

How copy file from one path to another path in PHP?

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.


2 Answers

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

like image 197
riha Avatar answered Sep 18 '22 17:09

riha


Write an empty string as the content of filename.txt:

file_put_contents('filename.txt', '');
like image 43
Dan Grossman Avatar answered Sep 18 '22 17:09

Dan Grossman