943,754 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 9210
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 14th, 2008
0

Displaying images from mysql database

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
okonjiaustin is offline Offline
7 posts
since Dec 2003
Feb 14th, 2008
0

Re: Displaying images from mysql database

basically you want to do something like this:

PHP Syntax (Toggle Plain Text)
  1.  
  2. if ($_REQUEST[your_image_var] == 1) {
  3. header("Content-type: image/jpeg");
  4. print $bytes;
  5. exit ();
  6. }
Reputation Points: 12
Solved Threads: 19
Posting Pro
techniner is offline Offline
521 posts
since May 2005
Feb 15th, 2008
0

Re: Displaying images from mysql database

Hello techniner ,

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

Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
okonjiaustin is offline Offline
7 posts
since Dec 2003
Feb 16th, 2008
0

Re: Displaying images from mysql database

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>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cmarkme is offline Offline
4 posts
since Feb 2008
Feb 17th, 2008
0

Re: Displaying images from mysql database

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.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
coffeepot! is offline Offline
11 posts
since Feb 2008
Feb 18th, 2008
0

Re: Displaying images from mysql database

Please an I have the code for the display of the images and row data.

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
okonjiaustin is offline Offline
7 posts
since Dec 2003
Feb 18th, 2008
0

Re: Displaying images from mysql database

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.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
coffeepot! is offline Offline
11 posts
since Feb 2008
Feb 18th, 2008
0

Re: Displaying images from mysql database

Quote ...
So you define the path to your image directory thus:

define("MAIN_IMAGE_STORE","../images/");
The OP is saving the images in a table.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 18th, 2008
0

Re: Displaying images from mysql database

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.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
coffeepot! is offline Offline
11 posts
since Feb 2008
Feb 18th, 2008
0

Re: Displaying images from mysql database

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!
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Class Error
Next Thread in PHP Forum Timeline: Hi Folks





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC