我有一个动态图像,它使用 GD 来添加一些叠加图像/文本。这将是 dynamicImage.php?firstName=Bob&lastName=Sacamano。我想被提示下载该文件,所以我创建了一个 download.php 文件作为中间人:
//Get the Arguments
$file .= "firstName=".filter_var($_GET['firstName'], FILTER_SANITIZE_STRING);
$file .= "&lastName=".filter_var($_GET['lastName'], FILTER_SANITIZE_STRING);
//get The File Size
$size = intval(sprintf("%u", filesize($file)));
//Header Info to Prompt for Download and name it a .jpg
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-disposition: attachment; filename=dynamicImage.jpg");
header("Content-Length: ".$size);
readfile($file, true);//.$file);
有2个问题,首先我得到这个错误:
PHP Warning: filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for dynamicImage.php?firstName=bob&lastName=Sacamano in /www/download.php on line 19
PHP Warning: readfile(dynamicImage.php?firstName=bob&lastName=Sacamano) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: No such file or directory in /www/download.php on line 25
看看它如何将 & 解析为 & ? 但不仅如此。如果我取出参数并离开 dynamicImage.php,它会提示我下载原始 php 文件。有没有办法让它运行 PHP 然后下载生成的图像?顺便说一句,我的 dynamicImage.php 以:
header("Content-Type: image/JPEG");
ImageJpeg ($bg);
imagedestroy($bg);
固定。我因此改变了我的dynamicImage.php:
if(isset($_GET['download'])){
header('Content-Description: File Transfer');
header('Content-Type: application/octet-image');
header("Content-disposition: attachment; filename=dynamicImage.jpg");
}else{
header("Content-Type: image/JPEG");
}
ImageJpeg ($bg);
imagedestroy($bg);