2012-04-04 67 views
0

我在php中動態生成圖像。該圖像具有固定的名稱。我想要一個按鈕或超鏈接並點擊該按鈕,用戶應該能夠導出圖像而不是右鍵單擊並保存爲圖像選項。問題是,如果是excel,pdf或doc文件,我可以指定文件和瀏覽器的路徑自動請求打開或保存選項,但對於圖像,它會在單獨的窗口中打開它們。我想要相同的對話框來保存圖像像其他文件,如excel,pdf。請幫助我。導出並保存圖像對話框

感謝

回答

0

您可以設置標題強制下載(對於PHP):

header("Content-Description: File Transfer"); 
header("Content-Disposition: attachment; filename=$file"); 
header("Content-Type: image/png"); 

然後你就可以讀取該文件

readfile($file); 
+0

thanks.it工作 – 2012-04-04 13:49:42

0

你可以做的是力圖像下載使用.htaccess如果您使用的是Apache或直接通過php使用標題標籤

PHP示例

$file = "PATH TO FILE" ; 
$fileName = basename($file); 
$fileSize = filesize($file); 
set_time_limit(0); 
header("Pragma: public"); 
header("Expires: 0"); // Cache Options 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // Cache Options 
header("Content-type: application/octet-stream"); 
header("Content-Disposition: filename=\" " . $fileName ."\""); 
header("Content-length: $fileSize"); 
readfile($file);