【发布时间】:2015-05-09 05:34:15
【问题描述】:
如果文件在 24 小时内最后一次修改,我希望以数字方式增加文件的内容,否则将文件的内容重置为 1。但是我想确保无论有多少用户同时访问该脚本,它都能继续工作时间(脚本总是需要执行,但要确保它不会错误地覆盖/计算 - 我相信这就是使用群发的地方)。
请看下面的代码:
$host_limit = 50;
$file = 'timer.txt';
$fh = fopen($file,'r+');
if (flock($fh,LOCK_EX)) {
$content = fgets($fh);
//FILE HAS NOT BEEN MODIFIED IN LAST 24 HOURS
if (strtotime('-24 hours') > filemtime($file)) {
$content = 1;
} else {
$content = ($content + 1);
}
fwrite($fh, $content);
fflush($fh);
flock($fh,LOCK_UN);
}
fclose($fh);
if ($content < $host_limit) {
//do stuff
}
上述工作是否可以如我所愿(因为无法模拟我期望测试的内容)?
【问题讨论】: