Bug #61271 [Opn->Nab]: filemtime() returns invalid timestamp when using SplFileObject
Edit report at https://bugs.php.net/bug.php?id=61271&edit=1
ID: 61271
Updated by: rasmus@php.net
Reported by: suinyeze at gmail dot com
Summary: filemtime() returns invalid timestamp when using
SplFileObject
-Status: Open
+Status: Not a bug
Type: Bug
Package: SPL related
Operating System: MacOSX 10.7.3
PHP Version: 5.4.0
Block user comment: N
Private report: N
New Comment:
PHP uses an internal stat cache for performance reasons. SplFileObject will stat
the file and cache the results. If you need to modify the file's stat data and
for some reason read it back in the same request (which is pretty rare) then you
need to clear the stat cache with:
clearstatcache(false,$file);
Put that after your touch() call there and you will get your expected result.
Previous Comments:
------------------------------------------------------------------------
[2012-03-04 09:48:57] suinyeze at gmail dot com
Description:
------------
Once a SplFileObject instance was createdââ, filemtime() does not return the latest
modification time of the file.
Test script:
---------------
filemtime.php
<?php
$file = '/tmp/test';
touch($file);
$fileObject = new SplFileObject($file); // If comment out this line, it works well.
touch($file, time() + 1);
echo 'time(): ' . date('Y-m-d H:i:s', time()) . PHP_EOL;
echo 'time() + 1: ' . date('Y-m-d H:i:s', time() + 1) .
PHP_EOL;
echo 'filemtime($file): ' . date('Y-m-d H:i:s', filemtime($file)) .
PHP_EOL;
echo 'filemtime($file)@subprocess: ';
passthru('php -r "echo date(\'Y-m-d H:i:s\', filemtime(\'/tmp/test\'))
. PHP_EOL;"');
Expected result:
----------------
$ php filemtime.php
time(): 2012-03-04 18:37:01
time() + 1: 2012-03-04 18:37:02
filemtime($file): 2012-03-04 18:37:02
filemtime($file)@subprocess: 2012-03-04 18:37:02
Actual result:
--------------
$ php filemtime.php
time(): 2012-03-04 18:37:01
time() + 1: 2012-03-04 18:37:02
filemtime($file): 2012-03-04 18:37:01
filemtime($file)@subprocess: 2012-03-04 18:37:02
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61271&edit=1
Thread (2 messages)