2012-06-07 31 views
1

因此,我設置它以便用戶可以通過點擊按鈕下載圖像(如果它託管在我的網站上),但它不適用於外部圖像。沒有先將它複製到我的服務器上的文件夾,是否可以使用外部圖像(使用URL)執行此操作?允許用戶下載外部圖像PHP

這是我用於我自己的網站上的圖像。

$str = $_GET['image']; 
$img_name = substr(strrchr($str, '/'), 1); 
$image = "../u/i/".$img_name; 

if (file_exists($image)) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($image)); 
    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($image)); 
    ob_clean(); 
    flush(); 
    readfile($image); 
    exit(); 
} 

任何幫助表示讚賞,謝謝。

回答

1

正如您在http://php.net/manual/en/function.readfile.php上看到的那樣,您可以使用外部URL。

如果已啓用fopen wrappers,則可以使用URL作爲具有此功能的文件名。有關如何 指定文件名的更多詳細信息,請參閱fopen()。請參閱Supported Protocols and Wrappers獲得 鏈接,以獲取有關各種包裝具有哪些功能的信息鏈接,其使用說明和關於它們可能提供的任何預定義變量的信息。

這是一個PHP設置:(代碼不需要更改即可)http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen