|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48421 stream_get_line() - allow $length to be optional
Submitted: 2009-05-28 22:42 UTC Modified: 2021-04-08 11:15 UTC
Avg. Score:4.4 ± 0.8
Reproduced:5 of 5 (100.0%)
Same Version:4 (80.0%)
Same OS:5 (100.0%)
From: ryan dot brothers at gmail dot com Assigned:
Status: Open Package: Streams related
PHP Version: Any OS: Irrelevant
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:
Solve the problem:
23 - 10 = ?
Subscribe to this entry?

 [2009-05-28 22:42 UTC] ryan dot brothers at gmail dot com
I believe stream_get_line() should allow the $length parameter to be optional by allowing a $length of -1 that would read the remaining buffer up to either the $ending delimiter or EOF.

Currently if you pass a $length of 0, the $length defaults to PHP_SOCK_CHUNK_SIZE (8192) in ext/standard/streamsfuncs.c.  If you pass a length of -1, a PHP Warning is generated.

Other 'similar' functions that allow length to be optional or -1 are fgets, stream_get_contents, and file_get_contents.

Also, the manual page for stream_get_line() states that this function is the same fgets() except for the handling of the delimiter - but another difference, at least right now, is that the $length parameter is required in stream_get_line(), but optional in fgets().

Reproduce code:
$fp = tmpfile();

fwrite($fp, str_repeat('a', 10000));

fseek($fp, 0);
echo strlen(stream_get_line($fp, 0));

fseek($fp, 0);
echo strlen(stream_get_line($fp, -1));


Expected result:

Actual result:
Warning: stream_get_line(): The maximum allowed length must be greater than or equal to zero


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2014-04-30 22:48 UTC]
-Status: Open +Status: Feedback -Package: Feature/Change Request +Package: Streams related -Operating System: Linux +Operating System: Irrelevant -PHP Version: 5.2.9 +PHP Version: Any
 [2014-04-30 22:48 UTC]
Is this something you are still interested in seeing?
 [2014-05-05 02:03 UTC] ryan dot brothers at gmail dot com
Yes, this would still be useful to me.  I've had to work around it in the meantime.  Thanks for your help.
 [2014-12-30 10:41 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2015-01-03 13:05 UTC] ryan dot brothers at gmail dot com
Yes, I am still interested in this.  It wouldn't let me re-open the bug.
 [2015-01-03 13:27 UTC]
-Status: No Feedback +Status: Open
 [2020-01-10 13:30 UTC] n at n0099 dot net
Please mention this 8192 default length in document, and also i hope there could be a -1 value for unlimited length.
 [2021-04-01 15:34 UTC]
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2021-04-01 15:34 UTC]
I've updated the docs accordingly[1].

Anyhow, wouldn't passing PHP_INT_MAX as $length solve that problem
for all practical purposes?

[1] <>
 [2021-04-04 12:23 UTC] ryan dot brothers at gmail dot com
Yes, using PHP_INT_MAX is how I've worked around it for now.
 [2021-04-08 11:15 UTC]
-Status: Feedback +Status: Open -Assigned To: cmb +Assigned To:
 [2021-04-08 11:15 UTC]
But still you like that parameter to be optional.  Okay, that is not unreasonable.
 [2022-05-06 10:39 UTC] crezasgov at aol dot com
The following pull request has been associated:

Patch Name: Patch download link directly from cron mails
On GitHub:
 [2022-05-06 10:40 UTC] crezasgov at aol dot com
The following pull request has been associated:

Patch Name: Prepare for longer commit hashes
On GitHub:
 [2022-05-06 10:41 UTC] maghsodid at yahoo dot com
The following pull request has been associated:

Patch Name: usability: link to user profiles also on icon and full name
On GitHub:
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jul 20 23:01:28 2024 UTC