I have a php file called gallery.php, which is a page for a specific image gallery. For security, user images are stored outside the root website.
To get the appropriate files for each user, I use the getimage.php file, which serves the images from their location. In general, the directory structure looks like this:
getimage.php is written as follows:
$imgString = realpath('/UserImages/' . $_SESSION['username'] . '/' . $_GET['img']);
if (!startsWith($imgString, '/UserImages/' . $_SESSION['username'] . '/')
|| !(endsWith(strtolower($imgString), '.jpg') || endsWith(strtolower($imgString), '.jpeg')))
{
header('HTTP/1.0 403 Forbidden');
die();
}
$img = file_get_contents($imgString);
header("Content-type: image/jpeg");
echo($img);
exit();
$_GET ['img'], , , ( ). , , realpath, , , script.
, getimage.php webroot , , , ( gallery.php, img ).
, getimage.php oustide public_html, , gallery.php. getimage.php :
<img src=getimage.php?img=IMG_FILENAME.jpg />
getimage.php , - public_html.
, : , getimage.php?