472,353 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.
Apr 25 '10 #1
5 12759
Atli
5,058 Expert 4TB
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
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
  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 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
@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...
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...
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...
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...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.