|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 11 22:00:01 2025 UTC |
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?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?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...