A simple example that allows images to be included in HTML without being in the web site's file path
I’ve looked in a pot-full of forums for the answer to this question:
How can I get the ‘src’ parameter of an HTML ‘img’ tag to refer to an image that is not in the web site’s path? I wanted to be able to do this so that images could be more secure and wouldn’t need to be replicated for multiple subdomains.
What I finally pieced together is very straightforward: the seeds of it were in several places, but here’s a working model all in one place.
(By the way, this has been tested with Fireefox, IE7, and Chrome. My web site runs under PHP 5 on an Apache server.)
I decided to keep my images in a folder called ‘help’ that is at a peer level with ‘public_html’. In ‘help’ I have another folder called ‘sample’
Here’s the original image tag: <img src=”sample/myPicutre.jpg”>
Here’s the modified tag: <img src=”index.php?helpimage=sample/myPicture.jpg">
Here’s how my modified index.php file looks
index.php
<?php
extract($_GET); // will contain the ‘helpimage’ variable
if(isset($helpimage)) // if this is a call for an image
{
$f=pathinfo($helpimage); // and if its an actual image file (hacker-resistance)
if($f['extension']=="jpg")
{
chdir("../help"); // jump up one level to the help folder
readfile($helpimage); // and send the image back to the browser.
return; // that’s it
}
}
?>