...

View Full Version : display all images outside web root folder



dalonesta
06-20-2010, 09:41 PM
Hi,

please show me the way on how to display all images that are inside a folder located outside the web-root. i have only been able to display one image repeatedly. for example, if i have 3 images inside the folder that is outside webroot, my browser shows the first image 3 times rather than showing different images. am stuck. help! thanks. see my code below.

Here is images.php


<?php
// Get our database connector
require("includes/zaza.php");

// Grab the data from our people table
$sql = "select * from mytable";

$result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());

$imgLocation = "./uploadz/";

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 />";
}

?>





Here is call_images.php



<?php
// Get our database connector
require("includes/zaza.php");

$imgLocation = "./uploadz/";

$sql = "select * from mytable";

$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();

}
?>




help me :(

DJCMBear
06-20-2010, 10:01 PM
have you tried using a double dot for example ../uploadz/

dalonesta
06-20-2010, 10:20 PM
DJCMBear, thank you for replying. This point you mentioned is not the problem. I only put that there to conceal my original access path.
thanks.

DJCMBear
06-20-2010, 10:28 PM
in your database are the images stored along side an ID?

dalonesta
06-20-2010, 10:57 PM
DJCMBear, yes image names are stored alongside an ID number.

DJCMBear
06-20-2010, 11:11 PM
then try this.

- images.php


<?php
// Get our database connector
require("includes/zaza.php");

$imgLocation = "./uploadz/";

// Grab the data from our people table
$result = mysql_query("select * from mytable") or die ("Could not access DB: " . mysql_error());
if(mysql_num_rows($result))
{
while ($row = mysql_fetch_array($result))
{
$imgName = $row["filename"];
$imgID = $row['id'];
$imgPath = $imgLocation.$imgName;
print "<img src=\"call_images.php?imgID={$imgID}\" alt=\"\" /><br/>{$row['id']} - {$imgName}<br />";
}
}
?>


- call_images.php

<?php
// Get our database connector
require("includes/zaza.php");

$imgLocation = "./uploadz/";
$imgID = mysql_real_escape_string($_GET['imgID']);
$result = mysql_query("select * from mytable WHERE `id` = '{$imgID}'") or die ("Could not access DB: " . mysql_error());
if(mysql_num_rows($result))
{
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();
}
}
?>

dalonesta
06-20-2010, 11:32 PM
DJCMBear, thanks a lot! It works perfectly as expected. But one more question. Which website or book can you recommend for me to learn php. Something like php tutorials. thanks.

DJCMBear
06-20-2010, 11:47 PM
lol this may sound like im calling you a dummy lol but im not its the name of the book lol try googling "php for dummies" or "php for dummies pdf" as that way you can download the eBook version. It contains alot of good information in there. Be sure to get the latest version which would be PHP 5.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum