php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49611 file_get_contents() automatically including headers in output
Submitted: 2009-09-20 18:00 UTC Modified: 2009-09-27 02:55 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: d dot reade at readesresidential dot com Assigned:
Status: Closed Package: Filesystem function related
PHP Version: 5.2.11 OS: CentOS 5.3 x86
Private report: No CVE-ID: None
 [2009-09-20 18:00 UTC] d dot reade at readesresidential dot com
Description:
------------
Since PHP 5.2.11 various scripts we use that utilise the file_get_contents function have been including the file header info in the output. This doesn't happen all the time, but it's happening enough to break our scripts, as well as 3rd party scripts such as DOMPDF.

This appears to be happening with any file type and was first noticed within minutes of installing PHP 5.2.11 today.

We operate another server running 5.2.10. This uses the exact same configuration, extensions and web code, but has none of these problems.

Reproduce code:
---------------
<?
$file = file_get_contents('image.jpg');
file_put_contents('newimage.jpg', $file);
?>

Expected result:
----------------
newimage.jpg file containing exact contents of image.jpg

Actual result:
--------------
// start of newimage.jpg
HTTP/1.1 200 OK
Date: Sun, 20 Sep 2009 16:29:16 GMT
Server: Apache
Last-Modified: Sun, 20 Sep 2009 16:01:15 GMT
Accept-Ranges: bytes
Content-Length: 67648
Cache-Control: max-age=2592000
Expires: Tue, 20 Oct 2009 16:29:16 GMT
Connection: close
Content-Type: image/jpeg

����^@^PJFIF^@^A^A^@^@^A^@^A^@^@��^@<CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 100

// followed by the rest of the code...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-20 18:04 UTC] d dot reade at readesresidential dot com
Just to clarify the header information is being automatically added to random images (not all) since PHP 5.2.11. I have seen a similar bug report, however my issue has occurred since installing PHP 5.2.11 today. Downgrading to PHP 5.2.10 fixes the problem and everything works as expected from that point on.
 [2009-09-20 19:55 UTC] pajoye@php.net
Can't reproduce on many servers/platforms. Double check your script or config, something could be wrong.
 [2009-09-20 20:28 UTC] d dot reade at readesresidential dot com
But the problem stops occurring as soon as I downgrade to PHP 5.2.10. No changes are being made to any configuration or extensions either.
 [2009-09-20 20:50 UTC] rasmus@php.net
The thing that doesn't make sense in your bug report is that your reproduce code just says:

$file = file_get_contents('image.jpg');
file_put_contents('newimage.jpg', $file);

There is no HTTP involved here, so where would the HTTP headers come from?  

 [2009-09-20 20:54 UTC] d dot reade at readesresidential dot com
Ah fair play, sorry for the confusion. It was an example of what functions I was using. The real code uses http:// to fetch the required image.
 [2009-09-20 21:04 UTC] rasmus@php.net
And you can reproduce it with a 2-liner like that?  I can't on my Linux box.

Try this on your system:

$img = file_get_contents('http://slowgeek.com/flat.png');
file_put_contents('flat.png',$img);

run it a couple of times from the command line.  Is your flat.png ever corrupted?  


 [2009-09-20 22:08 UTC] d dot reade at readesresidential dot com
That works fine, but it's a single file. The script I'm running is fetching up to 80 images from a remote source (based on the same network) and compiling into a PDF.

I rebooted the server a few minutes ago and tried again... The script completed no problem, fetched all images and compiled into a PDF. So I tried again, but it failed half way through. The reason it gave was imagecreatefrompng complaining one of the PNG files was corrupt. I checked the PNG file and it contained the header information:

HTTP/1.1 200 OK
Date: Sun, 20 Sep 2009 22:03:29 GMT
Server: Apache
Last-Modified: Tue, 15 Sep 2009 14:04:55 GMT
Accept-Ranges: bytes
Content-Length: 883
Cache-Control: max-age=2592000
Expires: Tue, 20 Oct 2009 22:03:29 GMT
Connection: close
Content-Type: image/png

�PNG

There were other files in the same folder (the /tmp folder) that were created as part of the same job, which compiled into temporary PNGs and JPEGs no problem, with no header information. This has only started happening since PHP 5.2.11.

I thought this was a bug since if I recompile Apache with PHP 5.2.10 instead leaving the configuration in tact, and try again, the script completes, again and again no problem. It never fails.

Is there any reason why PHP is adding header info to files instead of just the file contents?
 [2009-09-20 22:19 UTC] pajoye@php.net
Can you show us your configure line too please?
 [2009-09-20 22:21 UTC] d dot reade at readesresidential dot com
Sure, here it is:

'./configure' '--disable-pdo' '--enable-bcmath' '--enable-calendar' '--enable-ftp' '--enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes' '--enable-sockets' '--enable-zip' '--prefix=/usr/local' '--with-apxs2=/usr/local/apache/bin/apxs' '--with-freetype-dir=/usr' '--with-gd' '--with-imap=/opt/php_with_imap_client/' '--with-imap-ssl=/usr' '--with-jpeg-dir=/usr' '--with-kerberos' '--with-libxml-dir=/opt/xml2/' '--with-mcrypt=/opt/libmcrypt/' '--with-mysql=/usr' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-png-dir=/usr' '--with-t1lib=/usr' '--with-ttf' '--with-xpm-dir=/usr' '--with-zlib' '--with-zlib-dir=/usr'
 [2009-09-20 22:44 UTC] pajoye@php.net
I was thinking about a bug the curl stream wrapper, but no. I can't reproduce this problem either, no matter the web server/OS/platform.
 [2009-09-20 23:03 UTC] rasmus@php.net
It would be really helpful if you could create an actual script that reproduces this.  It isn't until the 8th comment on this bug that we learn that it only happens if you are doing a whole bunch of them together.  Your original reproducing script not only didn't have any network component, but it also didn't have anything about multiple connections.

How are you doing these multiple connections?  Just a big loop around file_get_contents?  The best way to report a bug is to simplify your problem down into a script that we can run ourselves and see the problem.  If we can see it, we can fix it.  Otherwise we are guessing as to what might be happening on your end.
 [2009-09-21 08:11 UTC] d dot reade at readesresidential dot com
I have managed to replicate the issue, multiple times. Here's the code:

<?
$array	=	array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);

foreach($array as $val) {
	
	$tmp	=	tempnam('/tmp', 'test_img_');
	
	$file	=	file_get_contents($url);
	
	file_put_contents($tmp, $file);
	
	$img	=	imagecreatefrompng($tmp);
	
	imagedestroy($img);
}
?>

In my case $url was the direct http:// path to the image, in this instance a high quality PNG over 1MB in size.

The script repeats 80 times (to mimic what my scripts are trying to do). After the first run it stated 15 out of 80 of the PNGs were invalid PNG files. On the second run it said 12 were invalid. The results are different each time.

The error generated by PHP is:

Warning: imagecreatefrompng() [function.imagecreatefrompng]: '/tmp/test_img_92M2J2' is not a valid PNG file in /home/readesre/public_html/dev/img.php on line 12

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/readesre/public_html/dev/img.php on line 14

Here's the list of files from the tmp folder:

-rw-------  1 nobody nobody 1778247 Sep 21 09:01 test_img_92M2J2
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_bHZKlf
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_btMJcI
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_DctHCw
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_dnosxg
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_DOZKw1
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_DxQcqp
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_f95art
-rw-------  1 nobody nobody 1778247 Sep 21 09:01 test_img_H5SukI
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_LcT3ss
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_lkEMhQ
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_lucjnA
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_nksxx7
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_To6cXV
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_vEzSrY
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_vqmVzZ
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_x8cIzF
-rw-------  1 nobody nobody 1778247 Sep 21 09:01 test_img_XzAwHt
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_ZA0s5W
-rw-------  1 nobody nobody 1778247 Sep 21 09:01 test_img_ZgkSty
-rw-------  1 nobody nobody 1777966 Sep 21 09:01 test_img_zVa5gj

This is only a snippet of the full list, but notice the file sizes. The files that are 1778247 in size contain the following header info:

HTTP/1.1 200 OK
Date: Mon, 21 Sep 2009 08:01:24 GMT
Server: Apache
Last-Modified: Fri, 18 Sep 2009 10:26:04 GMT
Accept-Ranges: bytes
Content-Length: 1777966
Cache-Control: max-age=2592000
Expires: Wed, 21 Oct 2009 08:01:24 GMT
Connection: close
Content-Type: image/png

�PNG

Followed by the rest of the PNG file...
 [2009-09-22 09:54 UTC] jani@php.net
I can not reproduce this. Please provide the full script that causes this, including the $url..
 [2009-09-25 16:03 UTC] d dot reade at readesresidential dot com
Sorry, the script is integrated into our own Web Framework which equates to tens of thousands of lines of code. :)

I didn't think this was relevant at the time as I thought it was PHP related only, however our server runs cPanel 11. Today my host built us a brand new server, with CentOS 5.3 and a CentOS kernel (as the other one was a Xen Guest kernel).

I tried running the script, pointing it at a PNG file hosted on our live box, and it completed no problem first time round. I tried again and again and each time it was successful. This was done running it via the PHP CLI but the remote server runs PHP 5.2.10. So I pointed the script to a PNG located on the local host running PHP 5.2.11, different IP address. Using the http:// address it failed, while using the direct path, i.e. /home/user... works fine, every time (as it should do since the header are related to HTTP traffic).

I've now renamed all our htaccess files to be sure nothing is being caused by our scripts, which also ensures none of our scripts are loading when the PNG is requested, but it still fails with the same errors.

I reset Apache and PHP configurations to defaults, or at least to the same default as cPanel specifies. Same error.

I can only think something is being done by cPanel/WHM to the config files to cause this problem, but again it's only been happening since PHP 5.2.11, thus why I thought it was a bug in the first instance.

I would like to see what cPanel come back with as one of their developers is looking into this now, so please could you keep the ticket open? I'll report back my findings soon.

Thanks.
 [2009-09-25 17:50 UTC] jd at cpanel dot net
I've done lots of testing on the system where this issue was originally noticed and I'm pretty certain this was caused by the changes to main/streams/xp_socket.c in revision 288034.

A fix was already committed to SVN:

root@web3 [~/cpanel-ticket-492829/PHP_5_2]# svn log -r288604 --verbose
------------------------------------------------------------------------
r288604 | dmitry | 2009-09-23 11:25:54 +0100 (Wed, 23 Sep 2009) | 2 lines
Changed paths:
   M /php/php-src/branches/PHP_5_2/main/streams/xp_socket.c
   M /php/php-src/branches/PHP_5_3/main/streams/xp_socket.c
   M /php/php-src/trunk/main/streams/xp_socket.c

Fixed ext/standard/tests/streams/stream_get_contents_002.phpt

------------------------------------------------------------------------

With that one-line change, the problem seems to go away.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 00:01:30 2024 UTC