Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

Storing then displaying image from database

images php coding issues or problems here.

Moderators: macek, egami, gesf

Storing then displaying image from database

Postby krabople » Mon Mar 26, 2012 4:25 am

Hi, wondering if somebody can tell me where I'm going wrong (I'm new to all of this). I have the following php code which uploads an image file into my database:

Code: Select all
//Connect to database
include 'Resources/Include/db.inc.php';

$tmp=$_FILES['image']['tmp_name'];
//get users IP
$ip=$_SERVER['REMOTE_ADDR'];

//Don't do anything if file wasn't selected
if (!empty($tmp)) {

//Copy file to temporary folder
copy($tmp, "./temporary/".$ip."");

//open the copied image, ready to encode into text to go into the database
$filename1 = "./temporary/".$ip;
$fp1 = fopen($filename1, "rb");

//record the image contents into a variable
$contents1 = fread($fp1, filesize($filename1));

$contents1 = addslashes($contents1);

//close the file
fclose($fp1);

$ftype = $_FILES['image']['type'];

//insert information into the database
if(!mysql_query("INSERT INTO LetterImages (Data,Type,LetterID,Page)"." VALUES ( '$contents1', '$ftype',1,1)")){
echo mysql_error();
}

//delete the temporary file we made
unlink($filename1);

}


This seems to work ok, as when I go to the LetterImages table there is now an additional row with a file in the blob field. I then have the following code which is supposed to display the image:

Code: Select all
$result=mysql_query("SELECT * FROM LetterImages WHERE LetterID=1 AND Page=1");

//fetch data from database
$sqldata=mysql_fetch_array($result);

$encoded=stripslashes($sqldata['Data']);
$ftype=$sqldata['Type'];

//tell the browser what type of image to display
header("Content-type: $ftype");

//decode and echo the image data
echo $encoded;


Instead of displaying an image, however, this just displays pages and pages of incomprehensible data. Can anybody tell me where I'm going horribly wrong?
krabople
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Mar 26, 2012 4:13 am

Re: Storing then displaying image from database

Postby freshnet » Mon Mar 26, 2012 10:17 am

You need to have your header as header("Content-type: image/jpg"); Is that what you're getting from your code?
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 252
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: Storing then displaying image from database

Postby krabople » Mon Mar 26, 2012 10:43 am

More or less. When I echo out the $ftype variable it says "image/jpeg" (obviously without the quotes). I've also tried hard coding the header exactly as you've written below but the result is still the same :(
krabople
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Mar 26, 2012 4:13 am

Re: Storing then displaying image from database

Postby freshnet » Tue Mar 27, 2012 2:44 am

This won't work if you've printed/echoed anything to your page before the header, have you done that? How about if you have just the header and the variable on a page and call that page from your first page, e.g.
Code: Select all
//page 1
$result=mysql_query("SELECT * FROM LetterImages WHERE LetterID=1 AND Page=1");

//fetch data from database
$sqldata=mysql_fetch_array($result);

$encoded=stripslashes($sqldata['Data']);
$ftype=$sqldata['Type'];
require 'page2.php';

//page 2
//tell the browser what type of image to display
header("Content-type: $ftype");
//decode and echo the image data
print $encoded;
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 252
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: Storing then displaying image from database

Postby krabople » Tue Mar 27, 2012 7:35 am

Thanks for this, however I've just tried it and still got the same result :(

I think I'm just going to change the way I'm doing things altogether and store the images in a folder and just the pathway in the database.
krabople
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Mar 26, 2012 4:13 am

Re: Storing then displaying image from database

Postby freshnet » Tue Mar 27, 2012 3:45 pm

that's generally what people advise unless you have to use blobs!
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 252
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada


Return to PHP coding => Images

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron