使用PHP和CURL下载受htaccess保护的文件

时间:2011-02-09 11:45:57

标签: php .htaccess curl file

我尝试使用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?

谢谢!

另外,如果我回显$ output,我得到这个:

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 )

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分:2)

似乎缺少CURLOPT_BINARYTRANSFER选项。您粘贴的输出为$info而不是$output顺便说一句。它显示下载已发生,download_content_length为27369。

答案 2 :(得分: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);

答案 3 :(得分: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);

感谢您的回答,特别是chx的CURLOPT_BINARYTRANSFER选项!