PHP filemtime function - “stat failed for”

前端 未结 3 2007
遇见更好的自我
遇见更好的自我 2021-01-17 23:48

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 let

3条回答
  •  被撕碎了的回忆
    2021-01-18 00:32

    file_exists internally uses the access system call which checks permissions as the real user, whereas filemtime uses stat, which performs the check as the effective user. 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.

    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

    return @filemtime($_template->getCachedFilepath());
    

    If $_template->getCachedFilepath() can be set to a dummy value such as false, use the following:

    $path = $_template->getCachedFilepath();
    if (!$path) return false;
    return @filemtime($path);
    

提交回复
热议问题