|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81396 phar:// file fflush breaks file write
Submitted: 2021-08-29 11:03 UTC Modified: 2021-08-30 12:43 UTC
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: dixyes at gmail dot com Assigned:
Status: Verified Package: PHAR related
PHP Version: 8.0.10 OS: irrelative
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2021-08-29 11:03 UTC] dixyes at gmail dot com
fflush file make strange result in phar files.

this occurs on PHP 7.4 also, tested in windows and linux

use php -n -dphar.readonly=0 test.php to test

Test script:

// make up the phar
$phar = new \Phar(__DIR__ . DIRECTORY_SEPARATOR . 'test.phar');
$phar->addEmptyDir('dir'); // may be irrelative

//$phar_uri = __DIR__ . DIRECTORY_SEPARATOR . 'file';
$phar_uri = 'phar://' . __DIR__ . DIRECTORY_SEPARATOR . 'test.phar' . DIRECTORY_SEPARATOR . 'dir/file';

// make a file
$f = fopen($phar_uri, 'w+');
fwrite($f, 'this is content ');
fflush($f); // comment out this, things will be ok
fwrite($f, "of file.\n");

// read it
$f = fopen($phar_uri, 'r');
$content = fread($f, 4096);
if ($content !== "this is content of file.\n") {
    throw new Exception("bad file context: $content vs 'this is content of file.\n'");
} else {
    echo "OK\n";

Expected result:

Actual result:
bad file context with strange things


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-08-30 12:43 UTC]
-Status: Open +Status: Verified
 [2021-08-30 12:43 UTC]
When fflush() is called, the complete phar is written to disk, and
during this operation the connection to the $phar_uri file pointer
is severed[1].  Further writes to $phar_uri are then ignored by
the phar, except for the phar entry's size increment.  This leads
to the garbage bytes at the end of the string.

It might not be possible to retain the connection with the file

[1] <>
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jun 21 15:01:29 2024 UTC