php.net |  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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
4 + 5 = ?
Subscribe to this entry?

 
 [2008-08-17 14:35 UTC] 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 :)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-20 00:03 UTC] jani@php.net
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);
?>
"
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 18:01:28 2024 UTC