將image的路徑作爲get參數傳遞給showImage.php腳本。
<?php $pathToPicture = "server/www/images/imagexyz1823014719102714123.png"; ?>
<img src="/resources/php/showImage.php?pathToPicture=<?php echo $pathToPicture;?>" >
在這裏你可以得到傳遞的變量從$_GET
數組:
<?php
header('Content-Type: image/jpeg');
readfile($_GET['pathToPicture']);
?>
我最好建議使用BASE64_ENCODE和BASE64_DECODE爲pathToPicture
用於這一目的。也不要公開像這樣公開你的圖像位置的整個路徑。有一個看看下面改進的代碼
<?php $pathToPicture = "imagexyz1823014719102714123.png"; ?>
<img src="/resources/php/showImage.php?pathToPicture=<?php echo base64_encode($pathToPicture);?>" >
<?php
$location = "server/www/images/";
$image = !empty($_GET['pathToPicture']) ? base64_decode($_GET['pathToPicture']) : 'default.jpg';
// In case the image requested doesn't exist.
if (!file_exists($location.$image)) {
$image = 'default.jpg';
}
header('Content-Type: '.exif_imagetype($location.$image));
readfile($location.$image);
?>
的可能的複製[如何通過使用GET而不型PHP變量?](http://stackoverflow.com/questions/6074699/how-to-pass-variables- in-php-using-get-without-type) – Chuck