php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61271 filemtime() returns invalid timestamp when using SplFileObject
Submitted: 2012-03-04 09:48 UTC Modified: 2012-03-04 15:16 UTC
From: suinyeze at gmail dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.4.0 OS: MacOSX 10.7.3
Private report: No CVE-ID: None
 [2012-03-04 09:48 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-04 15:16 UTC] rasmus@php.net
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.
 [2012-03-04 15:16 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 06:01:30 2024 UTC