Displaying images from mysql database

Reply

Join Date: Dec 2003
Posts: 7
Reputation: okonjiaustin is an unknown quantity at this point 
Solved Threads: 0
okonjiaustin okonjiaustin is offline Offline
Newbie Poster

Displaying images from mysql database

 
0
  #1
Feb 14th, 2008
I have successfully uploaded image to mysql database using a form: This is the code:

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("adim");

if(isset($_REQUEST['submit']))
{

$imgtype=$_FILES['uploadfile']['type'];
$name=$_REQUEST['name'];
$address=$_REQUEST['address'];
$dateofbirth=$_REQUEST['dateofbirth'];



if($imgtype=="image/jpeg" || $imgtype=="image/jpg" || $imgtype=="image/pjpeg" || $imgtype=="image/gif" || $imgtype=="image/x-png" || $imgtype=="image/bmp")
{

$image=$_FILES['uploadfile']['tmp_name'];
$fp = fopen($image, 'r');
$content = fread($fp, filesize($image));
$content = addslashes($content);
fclose($fp);
$sql="insert into img_tab1 (name,image,address,dateofbirth) values ('$name','$content','$address','$dateofbirth')";
$res=mysql_query($sql) or die (mysql_error());
}
}
?>

Now I need to display the row data including the image, can someone please offer a code that will solve the problem.

Thanks

Austin
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 514
Reputation: techniner is an unknown quantity at this point 
Solved Threads: 19
techniner techniner is offline Offline
Posting Pro

Re: Displaying images from mysql database

 
0
  #2
Feb 14th, 2008
basically you want to do something like this:

  1.  
  2. if ($_REQUEST[your_image_var] == 1) {
  3. header("Content-type: image/jpeg");
  4. print $bytes;
  5. exit ();
  6. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 7
Reputation: okonjiaustin is an unknown quantity at this point 
Solved Threads: 0
okonjiaustin okonjiaustin is offline Offline
Newbie Poster

Re: Displaying images from mysql database

 
0
  #3
Feb 15th, 2008
Hello techniner ,

Please expand the code so that I can understand it better.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: cmarkme is an unknown quantity at this point 
Solved Threads: 0
cmarkme cmarkme is offline Offline
Newbie Poster

Re: Displaying images from mysql database

 
0
  #4
Feb 16th, 2008
ive worked with images & mysql by uploading the file to (DIR)/upload then storing the directory only in mysql.

then using something like :-
<?php
$result=mysql_query("select * images where id = BLAH!");
$row=mysql_fetch_array($result);
?>

<html>
<body>
<img src="<?php echo $row[directory] ?>"/>
</body>
</html>
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 11
Reputation: coffeepot! is an unknown quantity at this point 
Solved Threads: 2
coffeepot!'s Avatar
coffeepot! coffeepot! is offline Offline
Newbie Poster

Re: Displaying images from mysql database

 
0
  #5
Feb 17th, 2008
I have also done a bit with CMS which requires image storage on a LAMP system. Why not simply store the images in an image directory and refer to the image location in the database field. Okay it requires extra work using the file system, but to be honest I found it easier in the long run. PHP has some excellent fast functions for managing directories/files. You then retrieve and display the image as cmarkme suggests.
Anything one man can make - another man will try to break.
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 7
Reputation: okonjiaustin is an unknown quantity at this point 
Solved Threads: 0
okonjiaustin okonjiaustin is offline Offline
Newbie Poster

Re: Displaying images from mysql database

 
0
  #6
Feb 18th, 2008
Please an I have the code for the display of the images and row data.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 11
Reputation: coffeepot! is an unknown quantity at this point 
Solved Threads: 2
coffeepot!'s Avatar
coffeepot! coffeepot! is offline Offline
Newbie Poster

Re: Displaying images from mysql database

 
0
  #7
Feb 18th, 2008
Way I see it is this - you need four parts to your code. For preference I'd put things like database connections and paths to file storage in constants that you can include wherever you need them. The objective is to store your images in fixed directory, give them unique names if you need to, refer to the directory using a constant (so you can use it anywhere in your script) Store only the filenames in your database then when you need an image, you simply rebuild the path and filename by recombining the constant with the result of your SQL statement.

So you define the path to your image directory thus:

define("MAIN_IMAGE_STORE","../images/");

you then need to upload and store images in this directory thus:

$whereto=MAIN_IMAGE_STORE.$_FILES['ulfile']['name'];
$result=copy($_FILES['ulfile']['tmp_name'],$whereto);

(don't forget - never trust anything uploaded to your server!!!)

you also need to extract the filename from the uploaded file thus:

$image_name=$_FILES['ulfle']['name'];

(you can do some checks here to see if the image-name already exists and inform your user accordingly)

You store this filename ONLY in the database. (You do not need to store the path - that is already set in your path constant - the beauty is if you need to change the path, you do so simply by creating the new directory and updating the constant.)

"UPDATE database_table SET my_image = '$image_name WHERE ... etc

To retrieve the image for display you create your SQL to fetch the required filename from the database, "SELECT my_image FROM database_table WHERE guid = xxxx.xxxx...." or whatever, and include this result into your IMG tag, see the code snippet given by cmarkme. HOWEVER this time you need to specify the location of the image by rebuilding the path from your constant plus the image-name you have retrieved from the database; which is of course (simplified as):

$image_name=$row['image_name'];
or...
$full_path=MAIN_IMAGE_STORE.$image_name;
or...
<img src='".MAIN_IMAGE_STORE.$image_name."' ... etc>

Obviously your choice of syntax is personal in the code snippets above. You could build the entire path and filename first, or simply concatenate the whole thing into one line of code.

There is loads of stuff on this topic in the php manual. I hope this helps, it's a bit stilted I know and there are so many possibilities, but this should point you in one direction that will work a treat and is fairly efficient both from handling the images and database frugality.
Anything one man can make - another man will try to break.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Displaying images from mysql database

 
0
  #8
Feb 18th, 2008
So you define the path to your image directory thus:

define("MAIN_IMAGE_STORE","../images/");
The OP is saving the images in a table.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 11
Reputation: coffeepot! is an unknown quantity at this point 
Solved Threads: 2
coffeepot!'s Avatar
coffeepot! coffeepot! is offline Offline
Newbie Poster

Re: Displaying images from mysql database

 
0
  #9
Feb 18th, 2008
Hiya nav33n, yes fully understood, just a different way of doing things I guess. As I said, there are many ways of accomplishing the same job. The OP revealed a problem with retrieval of the image and display. Using this method elegantly negates the problem by allowing insertion of a filename, constructed on-the-fly so to speak into the html img tag. Horses for courses.
Anything one man can make - another man will try to break.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Displaying images from mysql database

 
0
  #10
Feb 18th, 2008
Yep.. And also, thats the best way, storing the path of the image instead of the image file. Displaying 1 image(if stored in a table), can be done. But I have no clue how to fetch all the images from the table. hmm!
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC