A quick example of displaying the image in PHP via your script. For this example, let's say that you've stored your images in /usr/home/navtarainc/ (a directory not accessible via the web). Now, let's pull up test_image.jpg from that folder, using PHP:
<?php
$image_path = "/usr/home/navtarainc/";
$image_name = "test_image.jpg";
header("Content-type: image/jpeg");
readfile("$image_path.$ima
?>
If you're working with multiple types of images, you'll need to set the headers accordingly. You can do something like this to determine the file type:
<?php
$ext = substr($image_name, -3);
switch ($ext) {
case "jpg":
header("Content-type: image/jpeg");
break;
case "gif":
header("Content-type: image/jpeg");
break;
}
?>
You can add more extension to the switch statement if you need to (make sure to order the statement by whichever image types are going to be most common, so that it's as efficient as possible). Hope that helps!
Main Topics
Browse All Topics





by: TomeeboyPosted on 2007-04-09 at 08:10:25ID: 18876148
This shouldn't be too much of a problem. You could use .htaccess, but I think a better solution would be to store these image files in a folder OUTSIDE of the home directory for your website. This way, they cannot be accessed by Apache (so somebody cannot simply type the URL in and bring them up in a web browser), and can only be accessed by a PHP script that loads the images.
If you only want the images viewed through the applications search page, then that's the only place where you need to insert code that will display the images. However, once an image is displayed to a user in a web browser, there's really no stopping them from copying it. The best method there (if these are images that need protecting even after they are viewed with the search page) would be to put a watermark on them that cannot be easily removed.