2010-09-28 133 views
0

我有一個動態圖像,它使用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&amp;lastName=Sacamano in /www/download.php on line 19 
PHP Warning: readfile(dynamicImage.php?firstName=bob&amp;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); 
+0

警告來自/www/download.php中的第19行和第25行,但您的問題中的代碼只有13行,後面是3行。如果你粘貼的代碼已經完成,你的$ file變量是不正確的。在我看來,包含'firstnameBob & lastnameSacramento',這並不是一個有效的文件名。在我看來,你的例子並不完整。 – thomasmalt 2010-09-28 07:14:11

回答

0

如何在您的硬盤上調用文件?這是你應該將文件化()的價值。

0

你正試圖從通過HTTP PHP腳本中調用一個PHP腳本?非常Rube Goldbergy。

  1. 您可能需要完整的路徑,例如http://yoursite/dynamicImage.php,否則PHP文件處理函數會將其視爲文件系統調用而不是HTTP包裝調用。
  2. 的PHP manual狀態不支持HTTP包裝是統計,所以你將不能夠做到filesize("http://url")

我建議重構中dynamicImage.php代碼,以便例如&行動=下載將讓你下載的圖像文件,而不是JPEG。

+0

輝煌!我編輯了我的帖子以反映我所做的更改。它工作完美。 並感謝您讓我谷歌Rube戈德堡 – user446699 2010-09-28 17:17:20

3

我會嘗試一些肉添加到我的評論在初始答案。

試圖通過另一個腳本通過http單獨調用來下載你的文件是反向的,在複雜的一個簡單的問題。

它會更容易重構你原來的dynamicImage.php代碼放到一個函數。然後在download.php中將該文件包含爲庫,並使用dynamicImage.php中的函數返回帶有Content-disposition頭集的圖像。

或者你可以作爲第三個參數添加下載到dynamicImage.php腳本,只需添加內容處置頭輸出形式dynamicImage.php時參數設置。

另請參閱@ Novikov的答案。

0

我覺得你只得到了2個解決方案:

  1. 生成的圖片在第一時間與您dynamicImage.php並將其存儲到你的服務器,例如,鮑勃 - Sacamano.jpg。但如果你有很多照片需要生成,它就會成爲一個硬碟空間食客,不過,這是一個解決方案。
  2. 正如Novikov和其他人所說的,您可能想要在同一個腳本中生成並強制下載。 (也許與你的http調用中的另一個參數一樣建議)。