PHP filemtime function - "stat failed for"

I have a problem with the PHP filemtime function. In my webapp, I use the Smarty template engine with a cache option. In my webapp, I can do some actions that generate an error, but allow me to focus on only one action. When I click the link on the page, the content is updated - I can click several times, and everything is fine, but about one request for 10 failed. The following error occurred:

filemtime() [<a href='function.filemtime'>function.filemtime</a>]: stat failed for 

and the line causing the problem:

  return ($_template->getCachedFilepath() && file_exists($_template->getCachedFilepath())) ? filemtime($_template->getCachedFilepath()) : false ; 

As you can see, the file exists because it is verified.

The problematic line of code is included in smarty_internal_cacheresource_file.php (part of Smarty lib v3.0.6)

The application runs on a UNIX system, external hosting.

Any ideas? Should I post more details?

+8
unix php smarty
source share
3 answers

file_exists internally uses the access system call, which checks permissions as a real user, while filemtime uses a stat , which checks as an effective user. Therefore, the problem can be rooted in assuming an effective user == a real user that is not running. Another explanation is that the file is deleted between two calls.

Since both the result of $_template->getCachedFilepath() and the existence of a file can change between system calls, why do you call file_exists at all? Instead, I suggest simply

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

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

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

Using:

 Smarty::muteExpectedErrors(); 

Read this and this

+2
source share

I have used filemtime successfully without checking "file_exists" for many years. The way I have always interpreted the documentation is that FALSE should be returned from the filemtime file on any error. Then a few days ago something very strange happened. If the file does not exist, my Cron job completed with the result. The result was not in the output of the program, but in the output of Cron. The message was "file length exceeded". I knew that the Cron job ended in a filemtime statement because I sent an email before and after that statement. The after email never arrived.

I set the ex_file to a file to fix the cron job. However, this was not to be necessary. I still do not know what has been changed on the hosting server that I use. Several other Cron jobs started working the same day. I don't know yet if they have anything to do with filemtime.

0
source share

All Articles