Hi
How can I retrieve files (images) from a directory outside wwwroot and display them on a web-site using a PHP-script? I've seen lots of examples of using the "header"-directive, but I want the image to be embedded in a website. The purpose is to avoid people to enter the picture-URL in the web-browser and mass-download pictures. Any suggestions or pointers are welcomed
Example:
wwwroot: /local/www/
Image-dir: /local/images/
Retrieve files from outside wwwroot
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Retrieve files from outside wwwroot
Using header() to output the appropriate content headers, and a simple readfile() which accepts the full path to your files (assuming PHP can access them). There are a ton of examples in the comments of those two links.
You would then reference your script as a normal image, something like:
You would then reference your script as a normal image, something like:
Code: Select all
<img src="/path/to/your/script.php?file=file1.jpg">- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Retrieve files from outside wwwroot
You would need to have some sort of authentication (e.g. use PHP sessions and a simple login system) to prevent direct downloads from random visitors. If you want to prevent one user from downloading multiple images, you need to set some kind of counter for each user who has logged in so that they are limited to download too many pictures. If you don't want to limit that, but stop spam, then you could setup some kind of tokens in order to view images.espenbe wrote:The purpose is to avoid people to enter the picture-URL in the web-browser and mass-download pictures.
Re: Retrieve files from outside wwwroot
John: your suggestion worked very well, and I think I will stick to that one for a while. Thank you very much
kaisellgren: Thank you for valuable thoughts and ideas which I will try to follow up at a later stage.
kaisellgren: Thank you for valuable thoughts and ideas which I will try to follow up at a later stage.