PHP filemtime 函数-“统计失败”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7120793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP filemtime function - "stat failed for"
提问by lbednaszynski
I have a problem with PHP filemtime function. In my webapp I use Smarty template engine with caching option. In my webapp I can do some actions which generate error, but lets focus on only one action. When I click link on page some content is updated - I can click few times and everything is OK but about one request on 10 fails. Following error occurs:
我的 PHP filemtime 函数有问题。在我的 webapp 中,我使用带有缓存选项的 Smarty 模板引擎。在我的 web 应用程序中,我可以执行一些会产生错误的操作,但让我们只关注一个操作。当我点击页面上的链接时,一些内容会更新 - 我可以点击几次,一切正常,但大约 10 个请求失败。出现以下错误:
filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for
and the line that causes the problem:
以及导致问题的行:
return ($_template->getCachedFilepath() && file_exists($_template->getCachedFilepath())) ? filemtime($_template->getCachedFilepath()) : false ;
As you can see, file exists because it is checked.
如您所见,文件存在是因为它被选中。
Problematic line of code is included in smarty_internal_cacheresource_file.php
(part of Smarty lib v3.0.6)
有问题的代码行包含在smarty_internal_cacheresource_file.php
(Smarty lib v3.0.6 的一部分)中
App is run on UNIX system, external hosting.
应用在 UNIX 系统上运行,外部托管。
Any ideas? Should I post more details?
有任何想法吗?我应该发布更多细节吗?
回答by phihag
file_exists
internally uses the access
system call which checks permissions as the realuser, whereas filemtime
uses stat
, which performs the check as the effectiveuser. Therefore, the problem may be rooted in the assumption of effective user == real user, which does not hold. Another explanation would be that the file gets deleted between the two calls.
file_exists
内部使用access
系统调用作为真实用户检查权限,而filemtime
使用stat
,它作为有效用户执行检查。因此,问题可能源于有效用户==真实用户的假设,该假设不成立。另一种解释是文件在两次调用之间被删除。
Since both the result of $_template->getCachedFilepath()
and the existance of the file can change in between system calls, why do you call file_exists
at all? Instead, I'd suggest just
既然$_template->getCachedFilepath()
文件的结果和存在都可以在系统调用之间改变,你为什么要调用file_exists
呢?相反,我建议只是
return @filemtime($_template->getCachedFilepath());
If $_template->getCachedFilepath()
can be set to a dummy value such as false
, use the following:
如果$_template->getCachedFilepath()
可以设置为虚拟值,例如false
,请使用以下内容:
$path = $_template->getCachedFilepath();
if (!$path) return false;
return @filemtime($path);
回答by Jeff
I used filemtime successfully without checking "file_exists" for years. The way I have always interpreted the documentation is that FALSE should be returned from "filemtime" upon any error. Then a few days ago something very weird occurred. If the file did not exist, my Cron job terminated with a result. The result was not in the program output but rather in the Cron output. The message was "file length exceeded". I knew the Cron job ended on the filemtime statement because I sent myself an email before and after that statement. The "after" email never arrived.
多年来,我成功地使用了 filemtime,而没有检查“file_exists”。我一直解释文档的方式是在出现任何错误时都应该从“filemtime”返回 FALSE。然后前几天发生了一件非常奇怪的事情。如果该文件不存在,我的 Cron 作业就会终止。结果不在程序输出中,而是在 Cron 输出中。消息是“超出文件长度”。我知道 Cron 作业以 filemtime 语句结束,因为我在该语句之前和之后给自己发送了一封电子邮件。“之后”电子邮件从未到达。
I inserted a file_exists check on the file to fix the Cron job. However, that should not have been necessary. I still do not know what was changed on the hosting server I use. Several other Cron jobs started failing on the same day. I do not know yet whether they have anything to do with filemtime.
我在文件上插入了 file_exists 检查以修复 Cron 作业。然而,这本不应该是必要的。我仍然不知道我使用的托管服务器上发生了什么变化。其他几个 Cron 作业在同一天开始失败。我还不知道它们是否与文件时间有关。