php mysql help--retrieving an image from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
salmander1
Forum Newbie
Posts: 4
Joined: Sun Oct 17, 2010 4:49 am

php mysql help--retrieving an image from database

Post by salmander1 »

hello there,
I am quite new to php....i am trying to develop this php page which uploads the image from the browser then it saves it in a mysql database. the data that is saved in database is saved as a mediumblob type but when I try to retrieve the image it doesnot shows an image instead a small box with a question mark in it.
this is my code:

Code: Select all

<html>
<head>
<title>Image Upload</title>
</head>

<body>

<form action="index.php" method="POST" enctype="multipart/form-data">
File: 
<input type="file" name="myfile">
<br>
<input type="submit" value="upload">
</form>

<?php
// conecting to database

mysql_connect("localhost","root","") or die("cant connect to server.");
mysql_select_db("databaseimage") or die("cant connect to database");



// file properties

/* here we are declaring a variable which uses a builtin function of $_FILES and the first part of it is the name of the image, which we have selected in input name ="image" section. and the second part is the temporary name and location where the file is being uploaded, usually its the temp folder of the server....you can check it by writing echo before the $file */

echo "name: ";
echo $name = $_FILES['myfile']['name'];
echo "<BR>";
echo "type ";
echo $type = $_FILES['myfile']['type'];
echo "<BR>";
echo "Size: ";
echo $size = $_FILES['myfile']['size'];
echo "<BR>";
echo "temp: ";
echo $temp = $_FILES['myfile']['tmp_name'];
echo "<BR>";
echo "error code: ";
echo $error = $_FILES['myfile']['error'];
echo "<BR>";
echo "<BR>";
echo "file contents: "; // this will be used to save in database as BLOB (Binary Large Object) data type.


echo $file_contents = addslashes(file_get_contents($_FILES['myfile']['tmp_name']));

echo "<BR>";

// now inserting the data into database

if (!$insert = mysql_query("INSERT INTO store VALUES ('','$name','$file_contents')"))
 echo "problem uploading file";
 
 else
 {

$last_id = mysql_insert_id();
echo "Image uploaded.";
echo "<BR>";
echo "<img src=get.php?id=$last_id>";

 }

?>
</body>
</html>
code for the get.php

Code: Select all

<?php


// conecting to database

mysql_connect("localhost","root","") or die("cant connect to server.");
mysql_select_db("databaseimage") or die("cant connect to database");



//echo $id = addslashes($_REQUEST['id']);
//echo $image = mysql_query("SELECT * FROM store WHERE id = $id");
//echo $image = mysql_fetch_assoc($image);
//echo $image = $image['image'];


$result = mysql_query("$insert");
header("Content-type: image/jpeg");
echo mysql_result($result, 0);
mysql_close($link);


//header("Content-length: $size");
//header("Content-type: image/jpeg");
//header("Content-Disposition: attachment; filename=$name");


//echo $image;
//exit ();



?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php mysql help--retrieving an image from database

Post by requinix »

Code: Select all

$result = mysql_query("$insert");
So where's this $insert supposed to be? I don't see it.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: php mysql help--retrieving an image from database

Post by internet-solution »

Your not sending a valid SQL to mysql -

Code: Select all

$result = mysql_query("$insert");
$insert is not defined.
salmander1
Forum Newbie
Posts: 4
Joined: Sun Oct 17, 2010 4:49 am

Re: php mysql help--retrieving an image from database

Post by salmander1 »

sorry this is the code that I am using... for get.php. The previous one was the testing one.

Code: Select all

<?php


// conecting to database

mysql_connect("localhost","root","") or die("cant connect to server.");
mysql_select_db("databaseimage") or die("cant connect to database");



$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM store WHERE id = $id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];


header("Content-length: $size");
header("Content-type: image/jpeg");


echo $image;




?>
salmander1
Forum Newbie
Posts: 4
Joined: Sun Oct 17, 2010 4:49 am

Re: php mysql help--retrieving an image from database

Post by salmander1 »

Anyone knows whats wrong with my code? Please help me...i really need help... :(
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php mysql help--retrieving an image from database

Post by requinix »

Code: Select all

header("Content-length: $size");
So where's this $size supposed to be? I don't see it.

Comment out the two header()s and look for error messages.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: php mysql help--retrieving an image from database

Post by cpetercarter »

Is there a special reason why you want to save the image in the database? A more normal arrangement is to save an image in an 'images' directory, and to use the database to save information about the image, such as its size, location, name etc.
salmander1
Forum Newbie
Posts: 4
Joined: Sun Oct 17, 2010 4:49 am

Re: php mysql help--retrieving an image from database

Post by salmander1 »

tasairis wrote:

Code: Select all

header("Content-length: $size");
So where's this $size supposed to be? I don't see it.

Comment out the two header()s and look for error messages.
$size is in index.html (the first script) as

Code: Select all

echo $size = $_FILES['myfile']['size'];
ok i commented out the two header's in the get.php file and this is what i got....dont understand the errors though....

Warning: mysql_query() [function.mysql-query]: MySQL server has gone away in C:\wamp\www\file-up_test\index.php on line 52

Warning: mysql_query() [function.mysql-query]: Error reading result set's header in C:\wamp\www\file-up_test\index.php on line 52

help me please.....i am really annoyed by this code now....i dont know why its not working!! :( :(
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php mysql help--retrieving an image from database

Post by requinix »

Storing BLOBs by putting the data directly inline with the query is risky. You're seeing one of the potential problems.

a) Use a prepared query and the 'b'lob parameter type. The mysql extension doesn't support it - you'll need another extension (like PDO or mysqli).
b) Do as cpetercarter suggested and store the images as files instead of in a database. I suggest it too.
Post Reply