此示例代码段应该按照您的要求执行。如果在服务器上启用魔术引号,我还包括仅剥离斜线的代码。这将使您的代码更具可移植性,并与未来版本的PHP兼容。我还添加了getimagesize()来检测MIME类型,以便为图像输出正确的标题,而不必假设它是特定类型。
if(isset($_GET['pic']))
{
//Only strip slashes if magic quotes is enabled.
$pic = (get_magic_quotes_gpc()) ? stripslashes($_GET['pic']) : $_GET['pic'];
//Change this to the correct path for your file on the server.
$pic = '/your/path/to/real/image/location/'.$pic;
//This will get info about the image, including the mime type.
//The function is called getimagesize(), which is misleading
//because it does much more than that.
$size = getimagesize($pic);
//Now that you know the mime type, include it in the header.
header('Content-type: '.$size['mime']);
//Read the image and send it directly to the output.
readfile($pic);
}
?>