Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP filemtime function - "stat failed for"

Tags:

php

unix

smarty

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:

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)

App is run on UNIX system, external hosting.

Any ideas? Should I post more details?

like image 326
lbednaszynski Avatar asked Aug 19 '11 11:08

lbednaszynski


2 Answers

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);
like image 81
phihag Avatar answered Oct 08 '22 05:10

phihag


Use:

Smarty::muteExpectedErrors();

Read this and this

like image 32
kbec Avatar answered Oct 08 '22 03:10

kbec