I tried to download files in a htaccess protected directory using php and curl. This is my code:
$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);
But this code does nothing... how can I initiate the download of file.pdf?
Thank you!
Also, if I echo $output, i get this:
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 开发者_JS百科[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 )
here is the working code, you made a mistake on the line with : 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);
There is a CURLOPT_BINARYTRANSFER
option that seems missing. The output you pasted is $info
not $output
btw. It shows the download happened, download_content_length
is 27369.
If you're trying to download a file, you should use CURLOPT_FILE
instead of doing a RETURNTRANSFER = true
. That'll write the downloaded data out to a file on your system directly, without flood PHP's memory space. Remember, PHP generally runs with a memory limit, and you can easily exceed it by downloading a large file.
Using RETURNTRANSFER
is only useful if you're going to be doing in-memory processing of the transferred data, such as loading HTML into the DOM system for parsing.
Try this instead:
$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);
$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);
This code works, just tried with excel, word and pdf document.
There is missing in the original code the curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
, the headers header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=file.pdf");
and the echo echo ($output);
.
Thanks for your answers, specially to chx for the CURLOPT_BINARYTRANSFER option!
精彩评论