php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64510 filemtime() and 'mtime' index from stat() only works for the first call
Submitted: 2013-03-25 13:46 UTC Modified: 2013-03-25 15:28 UTC
From: gu_ludo at yahoo dot com dot br Assigned:
Status: Wont fix Package: Filesystem function related
PHP Version: 5.3.23 OS: WinXP SP3
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: gu_ludo at yahoo dot com dot br
New email:
PHP Version: OS:

 

 [2013-03-25 13:46 UTC] gu_ludo at yahoo dot com dot br
Description:
------------
The functions stat() and filemtime() don't return a different value for the file's modification time on a second call whereas fstat() does.

Test script:
---------------
echo "Using filemtime():\n";
$filename = tempnam(sys_get_temp_dir(), 'foo');
file_put_contents($filename, "First row\n", FILE_APPEND);
var_dump(filemtime($filename));
sleep(5);
file_put_contents($filename, "Second row\n", FILE_APPEND);
var_dump(filemtime($filename));

echo "\nUsing stat():\n";
$filename = tempnam(sys_get_temp_dir(), 'foo');
file_put_contents($filename, "First row\n", FILE_APPEND);
$stat = stat($filename);
var_dump($stat['mtime']);
sleep(5);
file_put_contents($filename, "Second row\n", FILE_APPEND);
$stat = stat($filename);
var_dump($stat['mtime']);

echo "\nUsing fstat():\n";
$filename = tempnam(sys_get_temp_dir(), 'foo');
$f = fopen($filename, 'r');
file_put_contents($filename, "First row\n", FILE_APPEND);
$stat = fstat($f);
var_dump($stat['mtime']);
sleep(5);
file_put_contents($filename, "Second row\n", FILE_APPEND);
$stat = fstat($f);
var_dump($stat['mtime']);

Expected result:
----------------
Using filemtime():
int(1364217934)
int(1364217939)

Using stat():
int(1364217939)
int(1364217944)

Using fstat():
int(1364217944)
int(1364217949)

Actual result:
--------------
Using filemtime():
int(1364217934)
int(1364217934)

Using stat():
int(1364217939)
int(1364217939)

Using fstat():
int(1364217944)
int(1364217949)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-25 14:03 UTC] gu_ludo at yahoo dot com dot br
-Type: Bug +Type: Feature/Change Request
 [2013-03-25 14:03 UTC] gu_ludo at yahoo dot com dot br
I found out that there's the function clearstatcache() (http://php.net/manual/en/function.clearstatcache.php), which solves the problem.

Although, I think the "affected functions" listed on the documentation page should return by default the actual values and the caching could be optionally configured. So I change this bug's type to "Change request".
 [2013-03-25 15:01 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2013-03-25 15:01 UTC] nikic@php.net
We had a few discussions about this and the general consensus is that stats should by default be cached. Running scripts without stat cache can often result in a quite significant performance hit. The cases where you need non-cached stats are rather rare and certainly don't justify adding a slowdown for all other calls.
 [2013-03-25 15:28 UTC] gu_ludo at yahoo dot com dot br
Ok, so I'll will respect the consensus.
Just one question: May I use fstat() to non-cached stats, or it is likely to this 
function behave like stat() in the future?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 18:01:29 2024 UTC