Image retrieved is not being displayed in image format.

I tried to insert an image into database and retrieve it from the database. I have successfully inserted the image but it’s not displaying me the retrieved image. However I can see that the binary data of the image is being retrieved but it isn’t displaying in a image format. Here is the code:

upload.php :



<?php if(isset($_POST['submit'])) { $db=mysql_connect("localhost","root",""); mysql_select_db("picture",$db); $image=$_FILES["img"]["tmp_name"]; $name=addslashes($_FILES["img"]["name"]); $contents=addslashes(file_get_contents($_FILES["img"]["tmp_name"])); //echo $name." ".$contents; $res=mysql_query("insert into pics values('{}','{$name}','{$contents}')"); if($res==true) { echo "Done"; $lastid=mysql_insert_id(); echo ""; } else echo "Try again"; } ?>

getimg.php :

<?php $db=mysql_connect("localhost","root",""); mysql_select_db("picture",$db); $id=addslashes($_REQUEST['id']); $res=mysql_query("select * from pics where id=$id"); $res=mysql_fetch_assoc($ res); $res=$res['image']; header("Content-type:image/jpeg"); echo $res; ?>

Please help.

Well, Jayanth, we can help you do this, but, it is the absolute worst way to save images. Here are some
thoughts on your code…

First, you are using deprecated MySQL code. A lot of server no longer support that code. You need to do an
update to either MySQLi (i is for improved) or to PDO which is what a lot of programmers on this site use. I
do use MySQLi for small sites and it is easy to update from MySQL to MySQLi. (Only a couple minor
changes!)

Now, databases are not designed to house images very well. There is a lot of conversion issues and it is
very hard to display the image back to browsers once it is stored in a database. Experienced programmers
will never handle images that way. The standard way to handle images on a website is to create a folder
and name it something like “images” and store all of the images there. Then, just keep the name of the
image in the database. In that way, you can load the image quickly as needed using standard HTML and
it does not overpower the database services.

Occasionally, you need to set up a fancy way to handle the naming conventions for the images. Let’s say
you have 100 users and they all upload the same named picture… If the pictures are linked to a user, you
would need to code the image names with something like $user_id . “-” . $image_name so that you can
reload only the ones that came from that user for their display. Or, you can create a separate folder under
the images folder with the user’s ID or UserName and store all of their images in their own folder.

Either way you do this project, it is not a good idea to store images into a database. Let us know if you
really want to do it your way as it can be done, but, I hate to waste time on a bad programming procedure.

Oh, also, please place your code into PHP tags next time you post here. A different PHP tags for each file
or code group you post. ( Two for this last post… ) It helps us quickly move your code to our editors. Tx

Follow this steps:–

1)create a folder where other files are located ex upload.php
2)from upload.php file do code of image upload .
3)after image upload being successed ,insert into database
4)after that try to fetch image name and concate path of where you upload image (image path) and display image …
you will get best result…

Sponsor our Newsletter | Privacy Policy | Terms of Service