lazy saunter

(personal learnings in computation)

Tuesday, April 17, 2007

accessing files (from the web) not under the web root, part 3: jpg files

the third and last situation in which i wanted to hide raw data from the web yet allow files under the web root to access that data was the situation which initially stumped me.

i want to be able to display several images, in fact, all the images in an album, on the same web page yet i do not want a user to be able to navigate to the source directory for these images. i had considered storing the images in a database but thought storing them in the file system as files would be about 20 times easier for uploading, deleting, moving between albums, etc. when i did settle on file system storage, i realized i could not simply store the images in a directory outside of the web root and include the image with:

img src = "../../photos/photo_32"

nothing would display. later (see part 1 and part 2) i learned to make the directory housing the photos executable by the apache web server and to make the photos readable by apache. while this is all that is required for a text file or a php file, a further step must be taken for jpg files.

instead of including the path to the image file itself in the "img src" tag, the path to a php program which generates the image must be included. for example:

img src = "get_image.php?img=45"

the "get_image.php" program would consist of, at minimum, 3 lines of code to generate this image. what these functions do exactly i cannot explain. i suspect they convert binary data to a friendlier HTTP data format.

$img_handle = imagecreatefromjpeg("../../photos/img45.jpg");
header("Content-type: image/jpeg");
imagejpeg($img_handle);

4 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home