Retrieve images located outside web root using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • micmola
    New Member
    • Apr 2010
    • 4

    Retrieve images located outside web root using php

    Hi,

    I am developing a web application and I am stuck on the file upload section of my development. Can someone please put me through.

    I have the code that can upload images to a folder that is located outside the webroot as a security measure. I don't know how to display these images on a web browser. Please someone put me through.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    You can use the header function to set the appropriate header, and the readfile function to write the file to the output buffer.

    For example:
    [code=php]<?php
    $filePath = '/var/secure/images/myimage.png';

    header('Content-type: image/png');
    header('Content-length' . filesize($fileP ath));

    readfile($fileP ath);
    ?>[/code]


    This example includes a couple of security precautions as well, just to show how to do this safely.
    [code=php]<?php
    // The path where the images are stored.
    $imgLocation = '/var/secure/images/';

    // This fetches a file name from the URL,
    // named "image". E.G.
    // - example.com?ima ge=myimage.jpg
    // The "basename" function is there for
    // security, to make sure only a filename
    // is passed, not a path.
    $imgName = basename($_GET['image']);

    // Construct the actual image path.
    $imgPath = $imgLocation . $imgName;

    // Make sure the file exists
    if(!file_exists ($imgPath) || !is_file($imgPa th)) {
    header('HTTP/1.0 404 Not Found');
    die('The file does not exist');
    }

    // Make sure the file is an image
    $imgData = getimagesize($i mgPath);
    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('Content-type: ' . $imgData['mime']);
    header('Content-length: ' . filesize($imgPa th));

    // Print the image data
    readfile($imgPa th);
    ?>[/code]

    Comment

    • micmola
      New Member
      • Apr 2010
      • 4

      #3
      Thanks a million. I'm now able to upload and display an image. Thanks once again.

      Comment

      • micmola
        New Member
        • Apr 2010
        • 4

        #4
        I want to display all images on the web browser. I stored the filenames inside a mysql database. I am only able to display one image. Please help? Is there anything done wrongly?

        Code:
        // Get our database connector 
        require("includes/con.php"); 
        
        $sql = "select * from people";     
        $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error()); 
        
        while ($row = mysql_fetch_assoc($result)) 
        {     
            $imgLocation = './upload_folder/'; 
            $imgName = $row['filename']; 
                                                 
            $imgPath = $imgLocation . $imgName; 
        
             if(!file_exists($imgPath) || !is_file($imgPath)) { 
                   header('HTTP/1.0 404 Not Found'); 
                   die('The file does not exist'); 
             } 
        
             $imgData = getimagesize($imgPath); 
             if(!$imgData) {   
                    header('HTTP/1.0 403 Forbidden'); 
                    die('The file you requested is not an image.'); 
             } 
        
            header('Content-type: ' . $imgData['mime']); 
            header('Content-length: ' . filesize($imgPath)); 
        
            readfile($imgPath); 
        }

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          This method of displaying the image essentially turns the PHP request into an image. Therefore, each request can handle no more than a single image. If you try to print more than a single image, you corrupt the first one, or just pad the end of it with the additional images without actually getting them displayed.

          If you want to display more than one image per page using this method, you need to request the PHP script multiple times, one for each image. For example, if you were to put my last script into a file called "get_image.php" , you could do this to display three images in a HTML document.
          [code=html]<img src="get_image. php?image=first .jpg" alt="First image"><br>
          <img src="get_image. php?image=secon d.jpg" alt="Second image"><br>
          <img src="get_image. php?image=third .jpg" alt="Third image">[/code]

          You could use a similar page, created by a second PHP script, to display all the images from your database.

          Comment

          • micmola
            New Member
            • Apr 2010
            • 4

            #6
            Originally posted by Atli
            This method of displaying the image essentially turns the PHP request into an image. Therefore, each request can handle no more than a single image. If you try to print more than a single image, you corrupt the first one, or just pad the end of it with the additional images without actually getting them displayed.

            If you want to display more than one image per page using this method, you need to request the PHP script multiple times, one for each image. For example, if you were to put my last script into a file called "get_image.php" , you could do this to display three images in a HTML document.
            [code=html]<img src="get_image. php?image=first .jpg" alt="First image"><br>
            <img src="get_image. php?image=secon d.jpg" alt="Second image"><br>
            <img src="get_image. php?image=third .jpg" alt="Third image">[/code]

            You could use a similar page, created by a second PHP script, to display all the images from your database.
            What about if the filenames of the images are unknown to me and I am unaware of the number of images in the folder?

            I'm trying to create a script that prints all the images located inside a folder (beside webroot). Please help. I've been stuck on this for weeks.

            Comment

            Working...