Use PHP to display images

by on January 26, 2010

Q: How can I use PHP to display images on my website that are in a location on the server that the web server does not have access to? Since the web server does not have access to the folder I cannot use a regular HTML IMG tag.

A: There are a few things you need to remember when doing this. The first is that the web server might not have access to the folders/files to display them using a regular URL but the web server does have to have permission to access the folder for the script to get to the files inside of it. If there is a vulnerability in your code or in any other part of the site then you need to understand that not only the images in this “out of the way” directory will be accessible but anything else as well.

What you need to do is find an image/graphics library you like for PHP. There are some that are built in and there are some that are addons. Then all you will need to do is have PHP read in the image, then tell the browser you are going to send it an image, and then you just have to print out the image. In this case the user that asked the question gave us a good solution and here it is:

$myImage = imagecreatefromjpeg(‘jpgFile.jpg’);  // read in the image
header(“Content-type: image/jpeg”);  // tell the browser that you are sending an image
imagejpeg($myImage);  // write out the image to the browser
imagedestroy($myImage);  // clean up

Read Question Here