php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24456 fgets returning different result
Submitted: 2003-07-02 06:25 UTC Modified: 2003-07-02 06:29 UTC
From: chris dot baxter at ic24 dot net Assigned:
Status: Not a bug Package: Sockets related
PHP Version: 4.3.1 OS: Windows XP and Redhat 7.2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: chris dot baxter at ic24 dot net
New email:
PHP Version: OS:

 

 [2003-07-02 06:25 UTC] chris dot baxter at ic24 dot net
Description:
------------
PHP 4.2.3 works as I would expect. When doing an fgets($string,3) I get 3 char's returned, in my instance ACK.

as from 4.3.1 up to and including the new 5.0b the same code only returns AC I then have to do a further fgets to collect the K of my packet.


Reproduce code:
---------------
<?php
	$connect_to = "localhost";
	print "Connecting To: $connect_to <br>";
	$fp = fsockopen ($connect_to,2100,&$error,$errdesc,60);
	$out_data = "This is a test. Expecting 'ACK' returned followed a string of data";
	fputs($fp,$out_data);
	$page = fgets($fp,3);
	print "in1: $page <br>";
	$page = fgets($fp,512);
	print "in2: $page <br>";
?>

Expected result:
----------------
in1: ACK
in2: The rest of my data

Actual result:
--------------
in1: AC
in2: KThe rest of my data

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-02 06:29 UTC] wez@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

Please read the manual carefully:

\"Returns a string of up to length - 1 bytes read from the file pointed to by handle. Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first).\"

versions < 4.3 were not following this behaviour.

However, what this really means is that your script is broken;  if you are expecting \"ACK\\r\\n\", you should be using fread() to read 5 bytes and then check the return value.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 13:01:33 2025 UTC