Re: Secure Image Storage

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Kevin Murphy wrote:
Hello all,

Following up with my success last week with putting downloadable files in a directory above the web root and then using a combination of fopen and stuff to download the file, I am now trying to do something similar with images.

However, what I am trying to do is to put an image file above the web root, then use PHP to display that image in the web page, and not download it. I have the feeling that this isn't possible (all solutions I've seen involve using header() function, which won't work since this is midway down the page), but I wanted to make sure. This will return the binary source of the file:

print file_get_contents($file_path);

but doesn't display the image. Is there any way to have this (or something else) generate the image?
You need to have a separate file that you link to like you would for the image, then that file must send headers and the binary contents of the image to the browser. You cannot send the image data with the HTML data.

have a link in your image tag like this.  <img src='image.php?img=something.jpg' />

then in the file image.php it sends headers like this:

<?php

$image_folder = '/path/to/images/';

$file = $images_folder . basename($_GET['img']);

$imagedata = @file_get_contents($file);

$length = strlen($imagedata);

header('Last-Modified: '.date('r'));
header('Accept-Ranges: bytes');
header('Content-Length: '.$length);
header('Content-Type: image/jpeg');

print($data);

exit;

?>

This is a quick example that was put together out of information from the header and readfile & passthru pages in the manual


--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
       and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
    by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux