Hi all,

I need help on how to retrieve images that are located inside a folder that is outside the webroot. I understand that uploading images to a folder outside the webroot is much secure. Please can anyone help on this.

    I am going to call that folder with images '/outimg'

    Run this script from your webroot:

    <?php
    phpinfo(32);
    ?>

    You will see one variable
    $_SERVER["DOCUMENT_ROOT"] ....... path

    If your web doccument root path is for example:
    C:/Program/Apache2.2/htdocs

    Then it is very proble path to your outside root images is:
    C:/Program/Apache2.2/outimg
    That is if that images folder is beside your /htdocs

    And the path to one image would be:
    C:/Program/Apache2.2/outimg/imagename.jpg

      Thanks for the advice.

      Another challenge I am facing is how to display these images on a web browser.
      Bear in mind that the images are located inside a folder that is outside the web-root (httpdocs)

      Your help will be very much appreciated.

      Thanks

        You will need a PHP script to serve up the desired file, and therefore it will probably need to be told which file via a URL query string variable, e.g.: "[noparse]http://yoursite.com/image.php?name=foo.bar[/noparse]"

        image.php:

        $imageDir = $_SERVER['DOCUMENT_ROOT'] . '../images/';
        $image = (!empty($_GET['name'])) ? basename($_GET['name']) : false;
        if($image !== false and file_exists($imageDir . $image))
        {
           header('Content-Type: image/jpeg');
           readfile($imageDir . $image);
           exit;
        }
        header("HTTP/1.0 404 Not Found");
        

        That's just a basic idea: it could be enhanced to use getimagesize() to determine the image mime-type, check for a session variable so that only logged in users can access it, etc.

          Thanks people, for your input. I am now able to upload and display an image.

            Good to hear that. Remember to mark this thread as resolved (if it is) using the thread tools.

              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?

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

              Thank you.

                I suggest that you create a separate script that, given the image path (e.g., using the query string), validates the given path and displays the image as you have done here. You then invoke this new script from this one, but in the loop. In fact, the invocation would just be a matter of displaying an image element with the appropriate URL.

                  7 days later

                  I have only been able to display the replication of one image, despite have more than one image in my folder. So if I have three images in my folder, my code is only able to show the first image 3 times. Please help me with this code. Thanks. I have posted my code for you to help.

                  Call_images.php

                  $imgLocation = '/upload/';
                  
                  $sql = "select * from myTable";
                  $result = mysql_query($sql) or die ("Could not access DB: " .    
                       mysql_error());			
                  
                  while ($row = mysql_fetch_array($result)) {
                  
                  	$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("Content-Type: " . $imgData['mime']);
                  	header("Content-length: " . filesize($imgPath));
                  
                  	// Print the image data
                  	readfile($imgPath);
                  	exit();
                  }
                  
                  ?>
                  

                  image.php

                  <?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/>";
                  
                  
                     }
                  
                                      ?>
                  
                    Write a Reply...