|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45842 Image does not get uploaded corrected to DB from a URL
Submitted: 2008-08-17 14:35 UTC Modified: 2008-08-20 00:03 UTC
From: trendboy at gmail dot com Assigned:
Status: Not a bug Package: Streams related
PHP Version: 5.2.6 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: trendboy at gmail dot com
New email:
PHP Version: OS:


 [2008-08-17 14:35 UTC] trendboy at gmail dot com
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 = "";

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);

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"


The REAL image:

The HALF IMAGE generated from the DB (the Half Image):

Crazy stuff :)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-20 00:03 UTC]
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.

$handle = fopen("", "rb");
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Tue Dec 05 14:01:27 2023 UTC