Rachel
Rachel

Reputation: 103407

PHP: Is there a command that can delete the contents of a file without opening it?

Is there any way to remove the contents of an file in php, do we have any php command that does that, I know unlink but I do not want to delete the file instead I just want to remove the contents of that file.

I have an file which I pass while called a getCurrentDBSnap function, it takes in the file from /home/test/incoming folder and populates currentDB table state into the file using fputcsv and puts back file to /home/test/outgoing.

Currently file stays in incoming folder and when I can call the function getCurrentDBSnap it would take the file and override with latest state of DB into it.

Q: My question is, is it possible instead of overwriting the file, we can remove the content of file after ever getCurrentDBSnap such that file in incoming folder would be always empty ?

Hope it makes sense :)

Upvotes: 4

Views: 7529

Answers (6)

GaNeSh
GaNeSh

Reputation: 21

file_put_contents($file_name, "");

This is the best and simple option for clearing the contents of the file without deleting the file.

Upvotes: 0

Gordon
Gordon

Reputation: 316969

ftruncate — Truncates a file to a given length

Example

$handle = fopen('/path/to/file', 'r+');
ftruncate($handle, 0);
fclose($handle);

But you have to open the file.

Upvotes: 5

Felix Kling
Felix Kling

Reputation: 816384

I don't know have you write the contents into the file, but if you use fopen and you empty the file first, you are basically doing what the w mode is doing anyway.

From the documentation:

'w': Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

So I really see no need in emptying it first as you probably will use the w mode anyway.

You should only empty it if your code relies on the fact that this file is empty or if it is somehow security relevant.

Upvotes: 2

Daniel Papasian
Daniel Papasian

Reputation: 16423

You can't modify a file without opening it. The simplest way of doing that is to open it and truncate it while you open it, which in PHP would look like this:

fclose(fopen($filename, "w+"));

If you're going to invoke a unix shell (which is what the system() call does), the simplest way is to redirect /dev/null to the file, so in PHP that would be:

system(">$filename </dev/null");

There's a lot more overhead to this approach but it can be generalized and translated to any language or environment where you have a shell. There are other answers on this thread that have you calling system() to rm and then touch to recreate the file - besides being non-atomic, that approach also scares me much more.

Upvotes: 0

ant
ant

Reputation: 22948

Perhaps this is work-arround if exec is allowed .. for ex on linux :

<?
shell_exec("rm -rf " . $filename " && touch ".$filename);
?>

This will remove your file and then create it again, so it will "appear" that you emptied the content, this is just from the top of my head, not something that should be used likely .. Or if using linux and have access to exec command I'd use awk or sed(better) to empty the file

Upvotes: 0

Byron Whitlock
Byron Whitlock

Reputation: 53861

Try file_put_contents($filename, "");

or

unlink($filename);
touch($filename);

Upvotes: 14

Related Questions