|
 |
|
 |
 |
04-04-2005, 02:19 PM
|
#1 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
|
Using PHP to retrieve images stored in MySQL
I'm storing a few images in my database (yes, I know both sides of the debate, and I know I can just store the images' URLs in the database and pull them that way. This is an experiement. I'm doing it this way if it's at all possible. Period.)
Can you tell I've had problems getting this question answer in other forums?
Anyhoo, I've got a script that uploads the images and file-type information:
PHP Code:
<?
$db = mysql_connect("mysql_location", "username","password");
mysql_select_db("database",$db);
if (isset($binary_File) && $binary_File != "none")
{
$fp=fopen($preimgdata,'rb');
$binary_File_type=exif_imagetype($preimgdata);
$midimgdata=fread($fp,filesize($preimgdata));
$data = addslashes($midimgdata);
fclose($fp);
$sql = "INSERT INTO images_table (imgdata, imgtype) VALUES ('$data', '$binary_File_type')";
$result = mysql_query($sql, $db);
echo "<font face=verdana size=2>The file was successfully added to our database.<P>";
}
mysql_close();
?>
which seems to work fine (I can see the data and type in the using a mySQL frontend)
and I'm trying to pull them back down using this file, called pulldown.php and using the convention pulldown.php?id=idnumber:
PHP Code:
<?php
$db = mysql_connect("mysql_location", "username","password");
mysql_select_db("themid",$db);
$sql = "SELECT imgdata, imgtype FROM filmat11 WHERE id=".$id;
$result = @mysql_query($sql, $db);
$data = @mysql_result($result, 0, "imgdata");
$type = @mysql_result($result, 0, "imgtype");
header("Content-type: $type");
echo $data;
?>
The problem is, this produces, repeatedly, a broken image. Any ideas?
__________________
|
|
|
04-04-2005, 02:39 PM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,398
|
i would have to be on the 'dont store images in db' side  .. but let me ask. what field type is the mysql field you are storring the image in?
also, are you sure the mime type is correct? i.e. image/jpeg for a jpg file.
__________________
testing 1 2 3
|
|
|
04-04-2005, 02:44 PM
|
#3 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
|
I just realized I'm typing this from work -- in Fullerton!
=+)
I'm storing it in a BLOB, and the the type gets pulled with my jpegs as image/pjpeg, but I've tried it forcing the header as well instead of pulling it from the type variable.
__________________
|
|
|
04-04-2005, 02:44 PM
|
#4 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,398
|
here's something else i dug up .. before you insert the image into the database, .. use addslashes and addcslashes.
PHP Code:
$data = addcslashes($data, "\0");
Quote:
|
the addcslashes function replaces NUL characters with a \0 code because MySQL treats this character as the end of a string.
|
__________________
testing 1 2 3
|
|
|
04-04-2005, 02:46 PM
|
#5 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,398
|
is images/pjpeg a typo?
that's cool. u probably work down the street from where i live .. (downtown) .. i work in south l.a. though .. soon working in mission veijo .. wish i worked in fullerton! hehe
__________________
testing 1 2 3
|
|
|
04-04-2005, 02:46 PM
|
#6 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
|
Hmm . . . I've already got addslashes . . .never even HEARD of addcslashes, but I'll try anything! I'll let you know.
__________________
|
|
|
04-04-2005, 02:48 PM
|
#7 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,398
|
keep addslashes, and try adding the addcslashes line below it.
__________________
testing 1 2 3
|
|
|
04-05-2005, 07:41 AM
|
#9 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
|
My thanks to you both. My heartfelt gratitude.
Neither idea worked.
=+)
Could it be setting on my site host? If so what would I ask them about?
__________________
|
|
|
04-05-2005, 05:42 PM
|
#10 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 637
|
$data = file('image.gif'); will not work because that's asci based.
use fopen(), fread() and fclose() to fill up $data
then
PHP Code:
mysql_query("INSERT INTO table VALUES ('".mysql_real_escape_string($data)."')");
Works perfectly on my system
for output of the data use
PHP Code:
header('Content-type: image/gif');
echo $data;
__________________
|
|
|
04-05-2005, 07:56 PM
|
#11 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
|
Solution
Actually, I believe both of you . . . because neither thing had anything to do with my problem.
The images were getting slashed (\) by my script when I inserted them. The problem is, due to a setting on my server, they were getting slashed already. Double slashes mean twice the nuttiness. Thanks to the tech support people at my web host for figuring this out. I just ripped out the addslashes, and voila!
Thanks for your help.
__________________
|
|
|
04-06-2005, 01:05 AM
|
#12 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 637
|
strange only POST and GET gets auto-slashed by PHP.
I never heard of auto-slashing files.
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 11:59 AM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|