micmola Posted May 4, 2010 Share Posted May 4, 2010 Hello all, Please I need help in displaying all my images stored in a folder located outside the web root. I have been trying this for over a month to no avail please help. I have included my code for you to help detect where I am wrong. When I run this code, only one image is repeatedly displayed. For example, if I have 5 images in my folder, the first image will be displayed 5 times. Thank you. Images.php <?php // Get our database connector require("includes/copta.php"); // Grab the data from our people table $sql = "select * from people"; $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error()); $imgLocation = " /uploadfile/"; while ($row = mysql_fetch_array($result)) { $imgName = $row["filename"]; $imgPath = $imgLocation . $imgName; echo "<img src=\"call_images.php?imgPath=" . $imgName . "\" alt=\"\"><br/>"; echo $row['id'] . " " . $imgName. "<br />"; } ?> call_images.php <?php // Get our database connector require("includes/copta.php"); $imgLocation = '/ uploadz/'; $sql = "select * from people"; $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error()); while ($row = mysql_fetch_array($result)) { $imgName = $row["filename"]; $imgPath = $imgLocation . $imgName; // Make sure the file exists if(!file_exists($imgPath) || !is_file($imgPath)) { header('HTTP/1.0 404 Not Found'); die('The file does not exist'); } // Make sure the file is an image $imgData = getimagesize($imgPath); if(!$imgData) { header('HTTP/1.0 403 Forbidden'); die('The file you requested is not an image.'); } // Set the appropriate content-type // and provide the content-length. header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: image/jpg"); header("Content-length: " . filesize($imgPath)); // Print the image data readfile($imgPath); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/200641-display-all-images-from-folder-outside-web-root/ Share on other sites More sharing options...
Muddy_Funster Posted May 4, 2010 Share Posted May 4, 2010 couple of things to try. [*]Stop selecting * from your databases (you get this one for free - especialy when you use it on two pages right after each other [*]mysql_fetch_array[''] returns a numeric header for each row. Use mysql_fetch_assoc[''] to use associated table filed headers. Quote Link to comment https://forums.phpfreaks.com/topic/200641-display-all-images-from-folder-outside-web-root/#findComment-1052885 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2010 Share Posted May 4, 2010 Well, it's kind of dumb the way you did it. The call_images.php will select from people and you loop through to display the image. Problem is, you didn't specify which one in your SQL so it will grab them all and since your while loop exits after the first iteration, it will always print the first image. Since you're passing the file path to the call_images.php, why aren't you using it? The path is right there. There's no need to run a query. I agree with Muddy_Funster that you shouldn't select * from your database table, unless the table only has 2 columns lol. couple of things to try. [*]Stop selecting * from your databases (you get this one for free - especialy when you use it on two pages right after each other [*]mysql_fetch_array[''] returns a numeric header for each row. Use mysql_fetch_assoc[''] to use associated table filed headers. 1. Select from tables, not databases. 2. mysql_fetch_array returns both if the optional parameter is not passed to specify the type. Quote Link to comment https://forums.phpfreaks.com/topic/200641-display-all-images-from-folder-outside-web-root/#findComment-1052890 Share on other sites More sharing options...
Muddy_Funster Posted May 4, 2010 Share Posted May 4, 2010 Ken, if I ever need to get symantics checked on anything I do - You da man! Quote Link to comment https://forums.phpfreaks.com/topic/200641-display-all-images-from-folder-outside-web-root/#findComment-1052893 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.