我有一個動態圖像,它使用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);
有兩個問題,首先,我得到這個錯誤:
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
看看它如何分析&到& amp; ?但不僅如此。如果我拿出參數,並離開dynamicImage.php,它會提示我下載原始的php文件。有沒有一種方法可以讓它運行PHP,然後下載生成的圖像?
header("Content-Type: image/JPEG");
ImageJpeg ($bg);
imagedestroy($bg);
FIXD: BTW我dynamicImage.php結尾。我這樣改變了我的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);
警告來自/www/download.php中的第19行和第25行,但您的問題中的代碼只有13行,後面是3行。如果你粘貼的代碼已經完成,你的$ file變量是不正確的。在我看來,包含'firstnameBob & lastnameSacramento',這並不是一個有效的文件名。在我看來,你的例子並不完整。 – thomasmalt 2010-09-28 07:14:11