Edit report at http://bugs.php.net/bug.php?id=53064&edit=1
ID: 53064 Comment by: crrodriguez at opensuse dot org Reported by: mraaijmakers at gmail dot com Summary: filsize return 0 after is_file Status: Open Type: Bug Package: Filesystem function related Operating System: Linux Ubuntu PHP Version: 5.2.14 Block user comment: N New Comment: Just for the sake of completeness, other than the already mentioned stale stat cache you are doing it wrong. fwrite does not imply fflush() and fclose() does not imply fsync() (and PHP does not fsync/fdatasync the file on close or provide direct functionality to do so unfortunately) Previous Comments: ------------------------------------------------------------------------ [2010-10-14 18:12:19] cataphr...@php.net read: negligible ------------------------------------------------------------------------ [2010-10-14 18:11:29] cataphr...@php.net That's just stale stat cache. This will work: <?php $filename = dirname(__FILE__) . '/test.txt'; $fh = fopen($filename, 'w+'); $isFile = is_file($filename); fwrite($fh, 'some data goes here!'); fclose($fh); clearstatcache(false, $filename); var_dump($isFile, file_get_contents($filename), filesize($filename)); I'm not closing because maybe there's a way to invalidate the cache for that file with a negligent performance penalty. ------------------------------------------------------------------------ [2010-10-14 08:38:22] mraaijmakers at gmail dot com Description: ------------ When using is_file() on a file which is open via fopen(), the filesize() function returns 0, even after fclose() is called; see test script. Test script: --------------- $filename = dirname(__FILE__) . '/test.txt'; $fh = fopen($filename, 'w+'); $isFile = is_file($filename); fwrite($fh, 'some data goes here!'); fclose($fh); var_dump($isFile, file_get_contents($filename), filesize($filename)); Expected result: ---------------- boolean true string 'some data goes here!' (length=20) int 20 Actual result: -------------- boolean true string 'some data goes here!' (length=20) int 0 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=53064&edit=1