Jump to content

Recommended Posts

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

}
?>

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  :P

[*]mysql_fetch_array[''] returns a numeric header for each row.  Use mysql_fetch_assoc[''] to use associated table filed headers.

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  :P

[*]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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.