2011-02-09 93 views
5

我試圖使用php和curl在htaccess保護的目錄中下載文件。這是我的代碼:使用PHP和CURL下載htaccess保護的文件

$username = "MyUsername"; 
$password = "MyPassword"; 
$url = "http://www.example.com/private/file.pdf"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
$output = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch); 

但是這段代碼什麼都不做......我該如何啓動file.pdf的下載?

謝謝!

另外,如果我回聲$輸出,我得到這個:

Array ([url] => http://www.example.com/private/file.pdf [content_type] => application/pdf [http_code] => 200 [header_size] => 264 [request_size] => 116 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.007898 [namelookup_time] => 0.006777 [connect_time] => 0.006858 [pretransfer_time] => 0.006922 [size_upload] => 0 [size_download] => 27369 [speed_download] => 3465307 [speed_upload] => 0 [download_content_length] => 27369 [upload_content_length] => 0 [starttransfer_time] => 0.007839 [redirect_time] => 0) 
+1

你的意思是「什麼也不做」?什麼是輸出以及`$ info`中返回的錯誤是什麼? – 2011-02-09 11:48:10

+0

它不會返回任何錯誤,在$ info返回200確定,但下載不會啓動... – nmarti 2011-02-09 12:25:42

回答

2

有一個CURLOPT_BINARYTRANSFER選項似乎失蹤。你粘貼的輸出是$info而不是$output順便說一句。它顯示發生了下載,download_content_length是27369.

1

如果您嘗試下載文件,則應該使用CURLOPT_FILE而不是執行RETURNTRANSFER = true。這會將下載的數據直接寫入系統中的文件,而不會佔用PHP的內存空間。請記住,PHP通常運行的內存限制,您可以輕鬆地通過下載一個大文件來超過它。

使用RETURNTRANSFER僅當您要對傳輸的數據進行內存中處理時纔有用,例如將HTML加載到DOM系統進行分析。

試試這個:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_FILE, "/name/of/file/to/write/to/on/your/machine"); 
$output = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch); 
0
$username = "MyUsername"; 
$password = "MyPassword"; 
$url = "http://www.example.com/private/file.pdf"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1); 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
$output = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch); 
header("Content-Type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=file.pdf"); 
echo ($output); 

此代碼的工作,只是試圖與Excel,Word和PDF文檔。

原始代碼中缺少curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);,頭文件header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=file.pdf");和迴音echo ($output);

感謝您的回答,特別是針對CURLOPT_BINARYTRANSFER選項的chx!

5

這裏是工作的代碼,你就用線犯了一個錯誤:curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);

$username = "MyUsername"; 
$password = "MyPassword"; 
$url = "http://www.example.com/private/file.pdf"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
$output = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch); 
header("Content-Type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=file.pdf"); 
echo ($output);