Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-05-2013, 11:24 PM   PM User | #1
Mehdi72
New Coder

 
Join Date: Jul 2007
Posts: 88
Thanks: 12
Thanked 0 Times in 0 Posts
Mehdi72 is an unknown quantity at this point
Question Cannot Display Image Uploaded into MySql DB

I've followed an online tutorial for storing an image into a MySQL database.

I have created a table (databaseimage) with three fields id (primary key, auto increment), name, image (mediumblob type).

The php is supposed to display the image after it has been uploaded but I get the problem uploading image from the if then else statement. Also the full image name including the name of the tmp dir is displayed but the php is only supposed to display the filename. Despite these issues I can see the images inside the database with the correct filenames and sizes!

PHP Code:
<html>
<head>
<title>Upload image</title>
</head>

<body>

<form action="imageupload.php" method="post" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="upload">
</form>

<?php

// connect to database

mysql_connect("host""username""password") or die(mysql_error());
mysql_select_db('dbname') or die(mysql_error());

// file properties
echo $file $_FILES['image']['tmp_name'];

if (!isset(
$file))
echo 
"Please select an image.";
else 
{
$image addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name addslashes($_FILES['image']['name']);
$image_size getimagesize($_FILES['image']['tmp_name']);

if (
$image_size==FALSE)
echo 
"That's not an image.";
else
{
if (
$insert mysql_query("INSERT INTO databaseimage VALUES ('','$image_name','$image')"))
echo 
"Problem uploading image";
else 
{
$lastid mysql_insert_id();
echo 
"Image uploaded<p />Your image:<p /><img src=get.php?id=$lastid>";
}
}
}
?>

</body>
</html>
PHP Code:
<?php

mysql_connect
("host""username""password") or die(mysql_error());
mysql_select_db('dbname') or die(mysql_error());

$id addslashes($_REQUEST['id']);

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

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

echo 
$image;

?>
Mehdi72 is offline   Reply With Quote
Old 01-06-2013, 05:20 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,042
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Is there a reason why you want to store the images in the database (BLOB) and
not store the images in a directory and use the table for storing the image filenames?

In other words, do you really need to store the images/photos in your database table?


.
mlseim is offline   Reply With Quote
Old 01-06-2013, 07:05 PM   PM User | #3
Mehdi72
New Coder

 
Join Date: Jul 2007
Posts: 88
Thanks: 12
Thanked 0 Times in 0 Posts
Mehdi72 is an unknown quantity at this point
Quote:
Originally Posted by mlseim View Post
Is there a reason why you want to store the images in the database (BLOB) and
not store the images in a directory and use the table for storing the image filenames?

In other words, do you really need to store the images/photos in your database table?


.
I'd like to know how to do it before exploring storing the images into a dir which looks more complicated and the images will relate to an individual's record in the database.

Last edited by Mehdi72; 01-06-2013 at 07:53 PM..
Mehdi72 is offline   Reply With Quote
Old 01-06-2013, 08:04 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,493
Thanks: 44
Thanked 438 Times in 427 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by mlseim View Post
Is there a reason why you want to store the images in the database (BLOB) and
not store the images in a directory and use the table for storing the image filenames?

In other words, do you really need to store the images/photos in your database table?
Believe it or not storing in a db can actually be a better idea. Say you delete a user from the DB and then their images from the disk. If you do a rollback, the users account can be restored but those images that were wiped from the system are gone for good!

Of course then you have the problem of a swelling mysql table
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 01-06-2013, 09:37 PM   PM User | #5
Mehdi72
New Coder

 
Join Date: Jul 2007
Posts: 88
Thanks: 12
Thanked 0 Times in 0 Posts
Mehdi72 is an unknown quantity at this point
Quote:
Originally Posted by tangoforce View Post
Believe it or not storing in a db can actually be a better idea. Say you delete a user from the DB and then their images from the disk. If you do a rollback, the users account can be restored but those images that were wiped from the system are gone for good!

Of course then you have the problem of a swelling mysql table
My problem is I can't get this code to work correctly!
Mehdi72 is offline   Reply With Quote
Old 01-07-2013, 02:14 AM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
To save an image to a database you should use SQL to read the file into the field like this:

Code:
INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file))
That way you do away with the PHP code to try to convert it from a file into data that can be inserted into the database. You can also use the SQL DUMPFILE command to write the field from the database directly into a file again. The file in the database should have all of the headers etc included with it (as happens when you use LOAD_FILE) as otherwise the image can be easily broken.

I assume the autoincrement field is there to allow for multiple images with the same name as otherwise the image name could be used as the primary key.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-07-2013, 02:18 AM   PM User | #7
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,493
Thanks: 44
Thanked 438 Times in 427 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by Mehdi72 View Post
My problem is I can't get this code to work correctly!
£20 (thats UK pounds) by paypal and I'll fix it fo you via teamviewer
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 01-07-2013, 02:29 AM   PM User | #8
Mehdi72
New Coder

 
Join Date: Jul 2007
Posts: 88
Thanks: 12
Thanked 0 Times in 0 Posts
Mehdi72 is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
To save an image to a database you should use SQL to read the file into the field like this:

Code:
INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file))
That way you do away with the PHP code to try to convert it from a file into data that can be inserted into the database. You can also use the SQL DUMPFILE command to write the field from the database directly into a file again. The file in the database should have all of the headers etc included with it (as happens when you use LOAD_FILE) as otherwise the image can be easily broken.

I assume the autoincrement field is there to allow for multiple images with the same name as otherwise the image name could be used as the primary key.

Parse error: syntax error, unexpected T_STRING in imageupload.php on line 36

Code:
if ($image_size==FALSE)
echo "That's not an image.";
else
{
if ($insert = mysql_query("INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file)))
echo "Problem uploading image";
else
Don't I need to declare the file variable?
Mehdi72 is offline   Reply With Quote
Old 01-07-2013, 02:30 AM   PM User | #9
Mehdi72
New Coder

 
Join Date: Jul 2007
Posts: 88
Thanks: 12
Thanked 0 Times in 0 Posts
Mehdi72 is an unknown quantity at this point
Quote:
Originally Posted by tangoforce View Post
£20 (thats UK pounds) by paypal and I'll fix it fo you via teamviewer
Thanks for the offer but this isn't work related. I'm not going to learn php if I pay to get problems fixed!
Mehdi72 is offline   Reply With Quote
Old 01-07-2013, 10:59 AM   PM User | #10
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
Quote:
Originally Posted by Mehdi72 View Post
Parse error: syntax error, unexpected T_STRING in imageupload.php on line 36

Code:
if ($image_size==FALSE)
echo "That's not an image.";
else
{
if ($insert = mysql_query("INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file)))
echo "Problem uploading image";
else
Don't I need to declare the file variable?
That you do and you need to fix the syntax of the snippet you posted. Instead of using code tags use PHP tags and it'll become pretty obvious what is wrong a good portion of the time.

PHP Code:
if ($image_size==FALSE) {
     echo 
"That's not an image.";
} elseif (
$insert mysql_query("INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file)")) {
     echo 
"Problem uploading image";
} else {
// do something else here if you need to

__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Reply

Bookmarks

Tags
image upload, mediumblob, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:56 PM.


Advertisement
Log in to turn off these ads.