NYCPHP Meetup

NYPHP.org

[nycphp-talk] Thanks in advance

Tim Lieberman tim_lists at o2group.com
Wed Dec 26 21:24:55 EST 2007


David Krings wrote:
> chad qian wrote:
>> I get the following source code from internet.It can upload the image 
>> but it can't display the image on the web page.There is always "x" 
>> sign .Any idea?Thanks a lot!
>
> Check the source code of the resulting page and see if the value for 
> the <img> tag points to a location from where the image can be 
> retrieved. When you see the X the browser cannot download the image as 
> the file is not found in the specified location.
More likely in this case, something is causing the image data that gets 
sent to the browser to be corrupted.  Note that he's storing the image 
in the database, and this very same script (with $_GET['gim'] set) is 
outputting those data.

It's a perennial debate, but I'll go there:  Storing image data in the 
database is a bad idea.  You already have a very robust database for 
managing files: the filesystem.  So before you bother debugging this 
code more, you might want to consider changing your implementation.  
Simply write a uniquely named file somewhere on disk, and store the 
filename in the database.  Then you can construct the relevant src 
attributes on image tags, etc, or if you must, run the image data 
through PHP using fpassthru() or similar.


Here are some things I'd look at, if I was debugging your code:

    1) When calling the script with "gim" in the URL (telling it to send 
the image), does any extra data get sent?  Are there spaces before your 
opening <?PHP tag, maybe?  Maybe something else in the code is causing 
warnings or notices to be issued?  If any of that garbage makes it into 
the output stream, you'll be ouputting a corrupt image.
    2) When storing the image in the database, are you doing anything 
that might corrupt it?  Maybe try using mysql_real_escape_string() 
instead of addslashes()

But as I said above, I've not even tried to do anything like this in 
years, since I figured out that it just makes your database sad to deal 
with lots of binary data.  Really -- consider using the filesystem to 
store the files, and the database to store the names of those files, and 
you avoid all of this headache.

-Tim




More information about the talk mailing list