473,473 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Retrieve images located outside web root using php

4 New Member
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
5 13289
Atli
5,058 Recognized Expert Expert
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
micmola
4 New Member
Thanks a million. I'm now able to upload and display an image. Thanks once again.
Apr 26 '10 #3
micmola
4 New Member
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
5,058 Recognized Expert Expert
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
micmola
4 New Member
@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

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

Similar topics

11
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...
2
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...
12
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...
9
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...
5
matheussousuke
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...
0
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...
0
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...
0
Oralloy
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,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.