php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53064 filesize return 0 after is_file
Submitted: 2010-10-14 08:38 UTC Modified: 2010-12-13 17:22 UTC
From: mraaijmakers at gmail dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.2.14 OS: Linux Ubuntu
Private report: No CVE-ID: None
 [2010-10-14 08:38 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-14 18:11 UTC] cataphract@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 18:12 UTC] cataphract@php.net
read: negligible
 [2010-10-15 03:15 UTC] crrodriguez at opensuse dot org
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)
 [2010-11-05 02:03 UTC] felipe@php.net
-Summary: filsize return 0 after is_file +Summary: filesize return 0 after is_file
 [2010-12-13 17:22 UTC] iliaa@php.net
-Status: Open +Status: Bogus
 [2010-12-13 17:22 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 17:01:29 2024 UTC