By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,464 Members | 1,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,464 IT Pros & Developers. It's quick & easy.

Retrieve images located outside web root using php

P: 4
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.
Apr 25 '10 #1
Share this Question
Share on Google+
5 Replies


Atli
Expert 5K+
P: 5,058
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $filePath = '/var/secure/images/myimage.png';
  3.  
  4. header('Content-type: image/png');
  5. header('Content-length' . filesize($filePath));
  6.  
  7. readfile($filePath);
  8. ?>

This example includes a couple of security precautions as well, just to show how to do this safely.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // The path where the images are stored.
  3. $imgLocation = '/var/secure/images/';
  4.  
  5. // This fetches a file name from the URL,
  6. // named "image". E.G.
  7. // - example.com?image=myimage.jpg
  8. // The "basename" function is there for
  9. // security, to make sure only a filename
  10. // is passed, not a path.
  11. $imgName = basename($_GET['image']);
  12.  
  13. // Construct the actual image path.
  14. $imgPath = $imgLocation . $imgName;
  15.  
  16. // Make sure the file exists
  17. if(!file_exists($imgPath) || !is_file($imgPath)) {
  18.     header('HTTP/1.0 404 Not Found');
  19.     die('The file does not exist');
  20. }
  21.  
  22. // Make sure the file is an image
  23. $imgData = getimagesize($imgPath);
  24. if(!$imgData) {
  25.     header('HTTP/1.0 403 Forbidden');
  26.     die('The file you requested is not an image.');
  27. }
  28.  
  29. // Set the appropriate content-type
  30. // and provide the content-length.
  31. header('Content-type: ' . $imgData['mime']);
  32. header('Content-length: ' . filesize($imgPath));
  33.  
  34. // Print the image data
  35. readfile($imgPath);
  36. ?>
Apr 25 '10 #2

P: 4
Thanks a million. I'm now able to upload and display an image. Thanks once again.
Apr 26 '10 #3

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. // Get our database connector 
  3. require("includes/con.php"); 
  4.  
  5. $sql = "select * from people";     
  6. $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error()); 
  7.  
  8. while ($row = mysql_fetch_assoc($result)) 
  9. {     
  10.     $imgLocation = './upload_folder/'; 
  11.     $imgName = $row['filename']; 
  12.  
  13.     $imgPath = $imgLocation . $imgName; 
  14.  
  15.      if(!file_exists($imgPath) || !is_file($imgPath)) { 
  16.            header('HTTP/1.0 404 Not Found'); 
  17.            die('The file does not exist'); 
  18.      } 
  19.  
  20.      $imgData = getimagesize($imgPath); 
  21.      if(!$imgData) {   
  22.             header('HTTP/1.0 403 Forbidden'); 
  23.             die('The file you requested is not an image.'); 
  24.      } 
  25.  
  26.     header('Content-type: ' . $imgData['mime']); 
  27.     header('Content-length: ' . filesize($imgPath)); 
  28.  
  29.     readfile($imgPath); 
  30. }
  31.  
  32.  
Apr 26 '10 #4

Atli
Expert 5K+
P: 5,058
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.
Expand|Select|Wrap|Line Numbers
  1. <img src="get_image.php?image=first.jpg" alt="First image"><br>
  2. <img src="get_image.php?image=second.jpg" alt="Second image"><br>
  3. <img src="get_image.php?image=third.jpg" alt="Third image">
You could use a similar page, created by a second PHP script, to display all the images from your database.
Apr 26 '10 #5

P: 4
@Atli
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.
Apr 29 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.