2010-09-24 102 views
1

我有一個指向圖像的頁面的鏈接列表。當我點擊某個圖像在瀏覽器中打開時,我需要將其下載(使用必須看到窗口以保存圖像)。正如我udnerstand,我必須製作腳本(僅限PHP),並在該鏈接地址中使用它,將圖像名稱傳遞給它。然後,以某種方式更改內容類型頁面並要求用戶下載文件。 你能幫我解決這個問題嗎?頁面內容類型

回答

1

PHP.net

<?php 
$file = 'monkey.gif'; 

if (file_exists($file)) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($file)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($file)); 
    ob_clean(); 
    flush(); 
    readfile($file); 
} 
?>