Page 1 of 1

display image from folder using path from database

Posted: Sat Feb 21, 2009 7:35 am
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";

}
};
}
}

?>

Re: display image from folder using path from database

Posted: Sat Feb 21, 2009 11:34 am
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="" />';