Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    display all images outside web root folder

    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 Code:
    <?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 Code:
    <?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

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    have you tried using a double dot for example ../uploadz/
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    in your database are the images stored along side an ID?
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #5
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    DJCMBear, yes image names are stored alongside an ID number.

  • #6
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    then try this.

    - images.php
    PHP Code:
    <?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 Code:
    <?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(); 
      }

    ?>
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • Users who have thanked DJCMBear for this post:

    dalonesta (06-20-2010)

  • #7
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •