Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view images from protected folder with php?

Tags:

php

I have a password protected directory (with .htaccess) on my website containing *.jpg files. I dont want that anyone can directly access these .jpgs - but I want to allow a php script to display the *.jpg files. Is something like that possible?


For those who wonder why I want this: I have a register form where a user can upload a picture and before finishing the registration he can check if the correct picture was uploaded. For the moment, I save the uploaded picture in a temporary directory and if he finishes it, I move the picture to the password protected directory. However, I dont like that in each registration there is a short time of period where the picture is public (e.g. through a search engine). Even worse, when someone does upload a picture but not complete the registration, then the picture will remain forever in the temp directory, unless I delete somehow. But if I set up a cron-job to delete all images in the temporary directory during a specific time, then it would be possible that someones picture will be deleteted if he registers at a unfavourable moment.

like image 810
Adam Avatar asked Dec 26 '22 13:12

Adam


1 Answers

h2ooooooo already answered my question in the comments section.

This is the code how it works, in my code I have to replace

<img src='link/to/protectet/picture.jpg'>

with

<img src='image.php'>

and the image.php consist only of

<?
header('Content-Type: image/jpeg');
readfile('link/to/protectet/picture.jpg');
?>

that worked. Thanks.

like image 51
Adam Avatar answered Jan 05 '23 05:01

Adam