php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72182 \SplFileObject::getSize() returns 0 for files created with tempnam()
Submitted: 2016-05-09 18:11 UTC Modified: 2016-05-09 18:14 UTC
Votes:11
Avg. Score:4.3 ± 0.7
Reproduced:11 of 11 (100.0%)
Same Version:2 (18.2%)
Same OS:3 (27.3%)
From: bishop@php.net Assigned:
Status: Open Package: SPL related
PHP Version: 5.5.0-5.6.21, 7.0.0-7.0.6 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2016-05-09 18:11 UTC] bishop@php.net
Description:
------------
\SplFileObject does not seem to treat file names created by tempnam() the same as file names created manually. In the latter case, \SplFileObject::getSize() reports the correct on-disk size during write operations as well as subsequent reads. The former case, however, returns 0 consistently.

HHVM appears to operate as expected.

See also https://3v4l.org/vLlb0

Test script:
---------------
$paths = [
    sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'bishop',
    tempnam(sys_get_temp_dir(), 'bishop'),
];
foreach ($paths as $path) {
    $fileW = new \SplFileObject($path, 'w');
    $fileW->fputcsv(range(0,2));
    assert(6 === $fileW->getSize());
    echo $fileW->getSize() . PHP_EOL;

    $fileR = new \SplFileObject($path, 'r');
    assert(6 === $fileR->getSize());
    echo $fileR->getSize() . PHP_EOL;
}


Expected result:
----------------
6
6
6
6

Actual result:
--------------
6
6
PHP Warning:  assert(): Assertion failed ...
0
PHP Warning:  assert(): Assertion failed ...
0

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-09 18:14 UTC] bishop@php.net
-Operating System: Linux 4.1.17-22.30.amzn1.x86_64 +Operating System: -PHP Version: 5.6.21 +PHP Version: 5.5.0-5.6.21, 7.0.0-7.0.6
 [2016-05-09 18:33 UTC] bishop@php.net
To clarify, if one adds clearstatcache() before the getSize() calls, then the output is as expected:

6
6
6
6

The gist of this bug report is that the behavior of getSize() for file names created by tempnam() and file names created manually should be the same. Both should require clearstatcache, or neither should require.
 [2019-04-02 07:27 UTC] david at curtis-whitfield dot co dot uk
Same behaviour replicated in the following environment;

PHP Version: 7.2.13
OS: Alpine Linux v3.8

As suggested, calling clearstatcache() before the call to getSize() resolved the issue.
 [2022-03-10 17:26 UTC] contact at dufresne dot dev
Welcome,

The same problem appeared with :

PHP Version: 7.4.28

Indeed, clearstatcache() resolved the issue.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 17:01:30 2024 UTC