PHP:是否有命令可以删除文件内容而无需打开文件? [英] PHP: Is there a command that can delete the contents of a file without opening it?

查看:78
本文介绍了PHP:是否有命令可以删除文件内容而无需打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以删除php中文件的内容,我们是否有任何执行此操作的php命令,我知道unlink,但我不想删除该文件,而只是想删除其中的内容该文件.

我有一个文件,该文件在调用getCurrentDBSnap函数时传递给我,它从/home/test/incoming文件夹中接收文件,并使用fputcsv将currentDB表状态填充到该文件中,然后将文件放回/home/test/outgoing中.

当前文件保留在传入文件夹中,当我可以调用函数getCurrentDBSnap时,它将获取文件并以DB的最新状态覆盖它.

问:我的问题是,是否有可能代替覆盖文件,所以我们可以在getCurrentDBSnap之后删除文件的内容,以使传入文件夹中的文件始终为空?

希望这是有道理的:)

解决方案

尝试 file_put_contents($filename, "") ;

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

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

解决方案

Try file_put_contents($filename, "");

or

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

这篇关于PHP:是否有命令可以删除文件内容而无需打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆