user396404
user396404

Reputation: 2819

Displaying an image in php from a file in a directory

I have several images in a directory which is located outside of the public facing portion of the server (outside of the public_html directory). I want to be able to read the image contents with a php file, then output the result to html.

For example, I have a php file called getimg.php with the following source code:

header('Content-type: image/jpeg');

$dir = '/home/server/images/';
$file_name = addslashes($_GET['file']);

readfile('' . $dir . '' . $file_name . '.jpg');

getimg.php reads the contents of a jpg image in the images directory, then is referenced using the following code when I want to display the image to a user:

<img src="getimg.php?file=name_of_image_file">

The getimg.php file also does some verification checks to ensure that the user is logged in, actually owns the image they are trying to view, etc (this is not shown above for simplicity's sake). This approach is meant to control access to the image files. However, that is not the matter at hand.

Here is the problem: The approach/code above is only working in the Chrome browser. The images are not properly displaying in IE, FF, Safari, or Opera. Any ideas why this isn't working or how to do this properly?

Notes:

UPDATE

The problem was not with the code itself. I had my php.ini file set to auto include a login verification script. Since I was not logged in all of my browsers, the image was not displaying. Silly mistake, but understandable and fixed. The code above should work just fine. It has also been updated to use readfile() instead of file_get_contents(). I am going to leave this post up for anyone that's browsing the web looking for a way to control who has access to an image using php.

Upvotes: 1

Views: 11223

Answers (3)

Decko
Decko

Reputation: 19375

Use the readfile function.

http://php.net/manual/en/function.readfile.php

Upvotes: 3

David Norris
David Norris

Reputation: 71

I've used the approach you've outlined previously and it does work. The fact that it works in Chrome suggests it's a browser issue. Three suggestions:

  1. Use the Firefox Live Headers extension or curl -I to see exactly what headers PHP is sending when getimg.php is requested. There could a Content-Encoding or gzip compression issue. You may also want to send headers such that the browser caches the image - by default PHP will send headers to prevent this.

  2. Use a RewriteRule so that you can use src="file_name.jpg" ie.

    RewriteRule ^(.+).jpg$ getimage.php?file=$1.jpg

  3. Use jQuery. jQuery always works.

Upvotes: 0

ariawan
ariawan

Reputation: 198

try this..

if($fileExt == 'jpg'){
$im = imagecreatefromjpeg($filePath);
    if ($im !== false) {
       header('Content-Type: image/jpeg');
       imagejpeg($im);
    }
}
if($fileExt == 'png'){
$im = imagecreatefrompng($filePath);
    if ($im !== false) {
       header('Content-Type: image/png');
       imagepng($im);
    }
}

if the image is gif, there is also imagecreatefromgif()

Upvotes: 2

Related Questions