#45842 [Opn->Bgs]: Image does not get uploaded corrected to DB from a URL

From: Date: Wed, 20 Aug 2008 00:03:56 +0000
Subject: #45842 [Opn->Bgs]: Image does not get uploaded corrected to DB from a URL
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-128241@lists.php.net to get a copy of this message
ID: 45842 Updated by: jani@php.net Reported By: trendboy at gmail dot com -Status: Open +Status: Bogus Bug Type: Streams related Operating System: Linux PHP Version: 5.2.6 New Comment: RTFM: "When reading from anything that is not a regular local file, such as streams returned when reading remote files or from popen() and fsockopen(), reading will stop after a packet is available. This means that you should collect the data together in chunks as shown in the examples below. <?php $handle = fopen("http://www.example.com/", "rb"); $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); ?> " Previous Comments: ------------------------------------------------------------------------ [2008-08-17 14:35:39] trendboy at gmail dot com Description: ------------ When you upload an image to a DB from a URL it seems as though only "half" the image is uploaded. (See image URLs below for working example) It is very odd. I've experienced the same issue by just saving it to a file so it seems it is not database related. Steps to recreate: 1) Create a page that uploads an image from a URL. (code below) 2) Create a page to view that image (code below) 3) Your image will only be "half uploaded" the image is half displayed yet a full image appears (just "white" half way). Reproduce code: --------------- Code of uploader: (feel free to use my test image, it's my server) // Database connection strings I've hidden here of course // // $tmpName = "http://www.irishtarot.com/images/header.jpg"; function getSizeFile($url) { if (substr($url,0,4)=='http') { $x = array_change_key_case(get_headers($url, 1),CASE_LOWER); if ( strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0 ) { $x = $x['content-length'][1]; } else { $x = $x['content-length']; } } else { $x = @filesize($url); } return $x; } $fp = fopen($tmpName, 'r'); $content = fread($fp, getSizeFile($tmpName)); $content = addslashes($content); fclose($fp); mysql_query("insert into thumbnail (image) values ('" . $content . "')"); Expected result: ---------------- The full image should be uploaded, but is isn't. It is almost like it "crashes" half way during the download and upload to the DB. At first I thought it was because the getSizeFile() method I created didn't wory so I echo'd it's size result but it is correct. So is there something wrong with fread() when used with a reference to a URL rather than a file?? Very confused :-) Actual result: -------------- "half image" Example: The REAL image: http://www.irishtarot.com/images/header.jpg The HALF IMAGE generated from the DB (the Half Image): http://www.guysonfilm.com/webmaster/previewad.html Crazy stuff :) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=45842&edit=1

« previous php.bugs (#128241) next »