php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #81345 ftell() after writing to compress://zlib stream wrong
Submitted: 2021-08-10 13:11 UTC Modified: 2021-08-10 13:11 UTC
From: cmb@php.net Assigned:
Status: Open Package: Streams related
PHP Version: 7.4Git-2021-08-10 (Git) OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
44 + 44 = ?
Subscribe to this entry?

 
 [2021-08-10 13:11 UTC] cmb@php.net
Description:
------------
After writing to a compress://zlib stream, ftell() reports the
number of uncompressed bytes which have been written, not the
actual stream position.  This is inherently tied to the way the
streams layer works: the stream's write method is supposed to
return the number of input bytes processed, so the same confusing
behavior happens for all streams for which the number of input
bytes is not the same as the number of output bytes.

Since this cannot easily be fixed, it should at least be
documented.


Test script:
---------------
<?php
$s = fopen("compress.zlib://file.gz", "w");
var_dump(fwrite($s, str_repeat("hello world", 100)));
fflush($s); // this doesn't make a difference
var_dump(ftell($s));
fclose($s);
var_dump(filesize("file.gz"));
?>


Expected result:
----------------
int(1100)
int(38)
int(48)


Actual result:
--------------
int(1100)
int(1100)
int(48)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-08-10 13:11 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2024-04-18 08:56 UTC] Anthony101990Jones at outlook dot com
It seems you’re encountering an issue where ftell() is reporting the number of uncompressed bytes written to a compress://zlib stream, rather than the actual stream position. This is a known behavior tied to the streams layer of PHP, where the stream’s write method is supposed to return the number of input bytes processed. This can be confusing because for streams where the number of input bytes is not the same as the number of output bytes, the position reported by ftell() will not match the actual stream position1.

As of the information available, this issue was documented as a bug and was open as of the last update. It’s recommended to check the PHP bug tracking system for the most recent updates on this issue1. In the meantime, if you need to know the actual position in the stream, you might have to use a different approach, such as manually tracking the bytes written or using a different compression stream wrapper that behaves as expected.

Here’s a brief example of the issue:
<?php
$s = fopen("compress.zlib://file.gz", "w");
var_dump(fwrite($s, str_repeat("hello world", 100)));
fflush($s); // this doesn't make a difference
var_dump(ftell($s)); // reports the number of uncompressed bytes
fclose($s);
var_dump(filesize("file.gz")); // actual compressed file size
?>
(https://github.com)(https://www.dognearme.co.uk/)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 09:01:27 2024 UTC