473,394 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Retrieve images located outside web root using php


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
5 13274
5,058 Expert 4TB

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';
  4. header('Content-type: image/png');
  5. header('Content-length' . filesize($filePath));
  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/';
  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']);
  13. // Construct the actual image path.
  14. $imgPath = $imgLocation . $imgName;
  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. }
  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. }
  29. // Set the appropriate content-type
  30. // and provide the content-length.
  31. header('Content-type: ' . $imgData['mime']);
  32. header('Content-length: ' . filesize($imgPath));
  34. // Print the image data
  35. readfile($imgPath);
  36. ?>
Apr 25 '10 #2
Thanks a million. I'm now able to upload and display an image. Thanks once again.
Apr 26 '10 #3
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
  2. // Get our database connector 
  3. require("includes/con.php"); 
  5. $sql = "select * from people";     
  6. $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error()); 
  8. while ($row = mysql_fetch_assoc($result)) 
  9. {     
  10.     $imgLocation = './upload_folder/'; 
  11.     $imgName = $row['filename']; 
  13.     $imgPath = $imgLocation . $imgName; 
  15.      if(!file_exists($imgPath) || !is_file($imgPath)) { 
  16.            header('HTTP/1.0 404 Not Found'); 
  17.            die('The file does not exist'); 
  18.      } 
  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.      } 
  26.     header('Content-type: ' . $imgData['mime']); 
  27.     header('Content-length: ' . filesize($imgPath)); 
  29.     readfile($imgPath); 
  30. }
Apr 26 '10 #4
5,058 Expert 4TB
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
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

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

Similar topics

by: LarryM | last post by:
Hi, NB, not to stop capturing the single displayed Image, but to stop downloading the entire image directory. (In my Website you will do a search, and get some thumbnails, and these can be...
by: Jeffry van de Vuurst | last post by:
Hi, (sorry for the crosspost, I wasn't sure which was the best place to put this). I was just thinking about something and wondered if any of you has some ideas about this. I'm using the...
by: Brian Henry | last post by:
I made a header control in asp.net and it references images in the /images/ folder in the virtural path's root folder... How can I get the header control to show the images at any level of the web...
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.