#23874 [Opn->Bgs]: fread() exits prematurely on files opened over HTTP
| From: | iliaa@php.net | Date: | Thu, 29 May 2003 18:23:06 +0000 |
| Subject: | #23874 [Opn->Bgs]: fread() exits prematurely on files opened over HTTP | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-40654@lists.php.net to get a copy of this message | ||
ID: 23874
Updated by: iliaa@php.net
Reported By: kier at vbulletin dot com
-Status: Open
+Status: Bogus
Bug Type: *Directory/Filesystem functions
Operating System: Redhat Linux 8.0
PHP Version: 4.3.2
New Comment:
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
You should be doing:
$contents = '';
while (($data = fread($fp, 99999999))) {
$contents .= $data;
}
A single fread() could get interupted by a signal and thus not fetch
all the data. By having the read code in a loop you protect yourself
against such a problem.
Better yet, use file_get_contents() to fetch the entire file in one go.
This function was introduced in PHP 4.3.0.
Previous Comments:
------------------------------------------------------------------------
[2003-05-29 12:16:48] kier at vbulletin dot com
After switching from PHP 4.3.2 RC2 to PHP 4.3.2 RC4 I noticed that a
lot of my scripts failed to properly open the files they were asked
to...
Here is an example script that works fine under PHP 4.3.2 RC2 but
breaks under 4.3.2 RC4 and later. I have not tried 4.3.2 RC3 so I can't
say whether the problem was introduced at the RC3 or RC4 stage.
Nonetheless, the problem still exists in PHP 4.3.2 Final.
<?php
$path = 'http://www.domain.com/some/large/page.html';
$fp = fopen($path, 'r');
$contents = fread($fp, 99999999);
echo '<pre>' . htmlspecialchars($contents) . '</pre>';
?>
This will output the HTML source of the page referenced in $path, but
under recent versions of PHP 4.3.2 the contents is truncated, having
not reached the end of the file.
It is worth noting that using
$contents = '';
while (!feof($fp))
{
$contents .= fgets($fp, 100);
}
does not exhibit the fread() truncation problem, and neither does using
fread() on LOCAL files. The problem seems isolated to opening remote
files over HTTP with fread();
I have verified this bug on several servers under different PHP
configurations.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=23874&edit=1