tags:

views:

71

answers:

5

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 :)

+2  A: 

Try file_put_contents($filename, "");

or

unlink($filename);
touch($filename);
Byron Whitlock
am using that but it does not delete the content of file present in the incoming folder ?
Rachel
so basically I have to over-write the file present in the incoming folder.
Rachel
Yes. that is the easiest way to create a zero length file.
Byron Whitlock
I really liked this solution, is it ok to use something like this in production or not
Rachel
its certainly safer than mine or Felixes
c0mrade
@Rachel totally okay for production
Byron Whitlock
@c0mrade: True. @Rachel: I still think there is no need in emptying it first ;) You gain no benefit. But it is different if your code relies on the fact that this file is empty.
Felix Kling
Oh ok. Thank you very much for all the information.
Rachel
@Felix: I did not got your point when you say `ut it is different if your code relies on the fact that this file is empty.` Can you elaborate on this ?
Rachel
@Rachel: I mean maybe you perform other operations on the file where the file should be empty. Or the file is tested for content and based a that it is treated differently.
Felix Kling
@Felix: No am not doing that anywhere and infact that is not at all the case of consideration so I probably would go with current approach of just overwriting it instead of emptying and writing into it again.
Rachel
A: 

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

c0mrade
Even easier (on linux): `shell_exec('echo > ' . $filename);`
Felix Kling
@Felix I agree, there is many ways to do this on linux .. and that is one of them
c0mrade
@c0mrade: Thank god that we have Linux :)
Felix Kling
rm -rf is downright dangerous. Why -r when we're expecting only a file, never a directory?Deleting the file has lots of negative side effects, as well. Besides the fact there will be a second it doesn't exist, you'll nuke the ownership/permissions/ctime of the file, and the inode number will change.
Daniel Papasian
@Daniel Papasian -f is force option so it will delete either file or file without any notification or confirmation, and I told him that it is dangerous, unless you got something helpful and innovating to say it would be better to stick to the minus vote than.
c0mrade
@c0mrade: No it won't. Even with `-f`, directories **will not** be deleted.
Felix Kling
A: 

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.

Daniel Papasian
+2  A: 

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.

Gordon
Will it delete the file ?
Rachel
@Rachel no. it will truncate the file to the given length. In the case of the given example to 0, which means the file will be emptied.
Gordon
+1  A: 

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.

Felix Kling
Actually am opening the file in the constructor of the class, and there can be read operation on file and write operation depending upon the type of file, I am passing this information from command prompt and am opening file with `r+w` permissions.
Rachel
I agree but I was just not sure of whether it was good practice to override as opposed to empty it first and write into it from production code point of view.
Rachel
@Rachel: Ok, I'am not quite sure how it behaves with `r+`. I mean the file handler is moved to the beginning of the file but when you actually write (assuming you write *less* data into the file as there already is) I don't know whether the remaining (old) content of the file is deleted or not. In this case it is indeed better to empty the file first to make sure no old data is in it.
Felix Kling