在使用文件时,使用PHP删除(取消链接)或创建(fwrite)文件

问题描述:

I am running a website with a LAMP system.

Contents come from a database.

For caching purposes, I create files on my webserver (containing cachable contents) (via fwrite()).

Every once in a while I am deleting the cache files (via unlink()).

File creation and deletion is done with a cronjob.

My question is: what happens when a visitor to my website is currently browsing (=requesting from webserver) file A.php and I am trying to write to or delete this very same file A.php.

To be precise: what happens if my cronjob issues the unlink() command and at the same time serves the file to the user.

Will this create a race condition?

I know how to retrieve the status from unlink and fwrite operations, so could I just loop over my unlink/fwrite for as long as these function return false (could not be deleted/written)?

Looking forward to reading your opinions/answers!

我正在运行一个带有LAMP系统的网站。 p>

内容来自 数据库。 p>

出于缓存目的,我在我的网络服务器上创建文件(包含可缓存的内容)(通过 fwrite() code>)。 p> \ n

每隔一段时间我就会删除缓存文件(通过 unlink() code>)。 p>

使用cronjob完成文件的创建和删除。 p>

我的问题是:当我的网站访问者当前正在浏览(=从网络服务器请求)文件A.php时,会发生什么,我正在尝试写入或删除此相同的文件A .php。 p>

准确地说:如果我的cronjob发出 unlink() code>命令并同时向用户提供文件,会发生什么。 p>

这会创建竞争条件吗? p>

我知道如何从unlink和fwrite操作中检索状态,所以我可以循环遍历unlink / fwrite 只要这些函数返回false(无法删除/写入)? p>

L 提前阅读你的意见/答案! p> div>

Yes, that obviously will create a race condition. The request for the file from the webserver and the deletion from the cron job are independent processes and there is no way for you to know if one or the other is doing something with the file.