Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > PHP
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 04-04-2005, 02:19 PM   #1 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
metazai is on a distinguished road
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($result0"imgdata");
  
$type = @mysql_result($result0"imgtype");
  
header("Content-type: $type");
  echo 
$data;
?>
The problem is, this produces, repeatedly, a broken image. Any ideas?
__________________
metazai is offline   Reply With Quote
Old 04-04-2005, 02:39 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,398
sde is on a distinguished road
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
sde is online now   Reply With Quote
Old 04-04-2005, 02:44 PM   #3 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
metazai is on a distinguished road
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.
__________________
metazai is offline   Reply With Quote
Old 04-04-2005, 02:44 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,398
sde is on a distinguished road
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
sde is online now   Reply With Quote
Old 04-04-2005, 02:46 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,398
sde is on a distinguished road
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
sde is online now   Reply With Quote
Old 04-04-2005, 02:46 PM   #6 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
metazai is on a distinguished road
Hmm . . . I've already got addslashes . . .never even HEARD of addcslashes, but I'll try anything! I'll let you know.
__________________
metazai is offline   Reply With Quote
Old 04-04-2005, 02:48 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,398
sde is on a distinguished road
keep addslashes, and try adding the addcslashes line below it.
__________________
testing 1 2 3
sde is online now   Reply With Quote
Old 04-04-2005, 03:14 PM   #8 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 637
DJMaze is on a distinguished road
nonono use mysql_real_escape_string() it's the only correct way to insert binary data inside MySQL
http://php.net/mysql_real_escape_string
__________________
DJMaze is offline   Reply With Quote
Old 04-05-2005, 07:41 AM   #9 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
metazai is on a distinguished road
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?
__________________
metazai is offline   Reply With Quote
Old 04-05-2005, 05:42 PM   #10 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 637
DJMaze is on a distinguished road
$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
__________________
DJMaze is offline   Reply With Quote
Old 04-05-2005, 07:56 PM   #11 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
metazai is on a distinguished road
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.
__________________
metazai is offline   Reply With Quote
Old 04-06-2005, 01:05 AM   #12 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 637
DJMaze is on a distinguished road
strange only POST and GET gets auto-slashed by PHP.
I never heard of auto-slashing files.
__________________
DJMaze is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Craigslist Anonymous Email Relay with PHP & MySQL? oc_earthling PHP 11 03-05-2005 08:10 AM
WML PHP and MySQL rmill9681 PHP 10 11-20-2004 12:59 PM
Simple PHP MySQL code: confused. easilyi Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 4 10-24-2004 07:53 PM
cant connect to mysql databases using php eran PHP 11 08-07-2004 08:02 AM
Help with setting up mySQL and PHP Ilya020 PHP 11 03-19-2003 05:10 AM


All times are GMT -8. The time now is 11:59 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle