php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #30734 stream_get_meta_data does not return the correct number of unread bytes
Submitted: 2004-11-09 15:52 UTC Modified: 2004-11-11 17:20 UTC
From: kormoc at gmail dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.0.2 OS: Linux
Private report: No CVE-ID: None
 [2004-11-09 15:52 UTC] kormoc at gmail dot com
Description:
------------
For a stream in blocking or non blocking mode, stream_get_meta_data does not return the correct number of unread bytes until it's been 'freshened' by a fread or similar.

For example, the test program.

The unread bytes will be 0, but if you insert a fgets($fp) above the stream_get_meta_data line, it will return the correct number of bytes. It also does this if blocking is set to false.

If you need any more information about my setup, feel free to ask, it's really not that odd or special.

Thanks!

Reproduce code:
---------------
<?PHP
$fp=fsockopen("irc.freenode.net",6667);
stream_set_blocking($fp,TRUE);
fwrite($fp,"NICK BOOT\nUSER BOOT 0 * :BOOT\n\n");
$read=array($fp);
while(1) {
if(false===($num_changed_streams=stream_select($read,$write=NULL,$except=NULL,1)))
  break;
 else  {
  $data=stream_get_meta_data($fp);
  print_r($data);
  while($data['unread_bytes']>0)   {
   echo fgets($fp);
   $data=stream_get_meta_data($fp);
  }
 }
}
fclose($fp);
?>

Expected result:
----------------
Something similar to this

kormoc@kormoc ~ $ php streamtest.php
NOTICE AUTH :*** Looking up your hostname...
Array
(
    [stream_type] => tcp_socket/ssl
    [mode] => r+
    [unread_bytes] => 0
    [seekable] =>
    [timed_out] =>
    [blocked] => 1
    [eof] =>
)
NOTICE AUTH :*** Found your hostname, welcome back
Array
(
    [stream_type] => tcp_socket/ssl
    [mode] => r+
    [unread_bytes] => 77
    [seekable] =>
    [timed_out] =>
    [blocked] => 1
    [eof] =>
)
NOTICE AUTH :*** Checking ident
NOTICE AUTH :*** No identd (auth) response
:sendak.freenode.net 432  BOOT :Erroneous Nickname
Array
(
    [stream_type] => tcp_socket/ssl
    [mode] => r+
    [unread_bytes] => 0
    [seekable] =>
    [timed_out] =>
    [blocked] => 1
    [eof] =>
)

Actual result:
--------------
Array
(
    [stream_type] => tcp_socket/ssl
    [mode] => r+
    [unread_bytes] => 0
    [seekable] =>
    [timed_out] =>
    [blocked] => 1
    [eof] =>
)
(repeating forever)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-11-09 17:06 UTC] wez@php.net
unread_bytes refers to the number of bytes in PHP's own internal buffer.
You should not use this value in a script, as is stated in the documentation.
 [2004-11-09 18:09 UTC] kormoc at gmail dot com
Actually, according to the docs here:
http://us2.php.net/manual/en/function.stream-get-meta-data.php

It does not tell you that, it says:
unread_bytes (int) - the number of bytes currently contained in the read buffer.

Nothing about it shouldn't be used. Infact in the comments on the page, they talk about a work around in PHP 4.x.

Could you please update the Documentation then to say this? Along with any other varables that function returns that shouldn't be used?

Thanks
 [2004-11-11 17:20 UTC] nlopess@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2020-02-07 06:11 UTC] phpdocbot@php.net
Automatic comment on behalf of nlopess
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=c31dd531112295f66b2adfa0a27e3a0a9097a941
Log: fix #30734: unread_bytes shouldnt be used
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 18 23:00:02 2025 UTC