display image from folder using path 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
mrtest17
Forum Newbie
Posts: 1
Joined: Sat Feb 21, 2009 6:53 am

display image from folder using path from database

Post by mrtest17 »

hi there, im new at this site and php
well i think i have a simple problem but i cant figure out whats wrong with my php code
i want to display an image from folder using the path that i have inserted from database.
i keep wondering whats wrong,
can someone help me out?
please!!!
tnx in advance

heres my code:

<?php
include("connect.php");
mysql_select_db("dbemail", $con);
$check = mysql_query("SELECT * FROM tblemail
WHERE username = '$_COOKIE[ID_my_site]'") or die(mysql_error());
while($row = mysql_fetch_array($check))
{
echo $row['photo'];
echo "<br>";

$dirname = "./images/thumbs/";
$images = scandir($dirname);
$ignore = Array(".", "..");
foreach($images as $curimg)
{
if(!in_array($curimg, $ignore))
{
if($row['photo'] == $dirname.$curimg)
{
echo "<img src='./images/thumbs/$curimg' /><br>\n";

}
};
}
}

?>
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: display image from folder using path from database

Post by watson516 »

When you are posting php to the forums, wrap it with [ code=php] [ /code] tags. This will provide syntax highlighting.

If you just want to grab an image path from the db and put it into an image tag, theres no need to read the dir unless you want to verify that it is indeed on the server. But users can only delete images if you allow them to so you'll know when one is gone and can change the db accordingly so theres really no need to verify the file exists.

Code: Select all

//connect to db and select table
$q="SELECT img_path from images WHERE img_id = 5";
$result=mysql_query($q);
$row=mysql_fetch_array($result);
echo '<img src="'.$row['img_path'].'" alt="" />';
Post Reply