【发布时间】:2017-08-21 12:39:25
【问题描述】:
我在从本地服务器下载 pdf 时遇到问题。我有一个文档文件的 url(例如:\10.10.10.10\directory\123_ABC.pdf)。我必须制作从 url 下载文件并在网络浏览器中显示的 php 函数。对此目录的访问受到保护(用户名和密码)。我不知道如何下载这个文件。也许卷曲?这可能吗?
我试过了:
$login = 'LOGIN';
$password = 'PASSOWRD';
$host = "http://10.10.10.10/directory/123_ABC.pdf";
$CurlConnect = curl_init();
curl_setopt($CurlConnect, CURLOPT_URL, $host);
curl_setopt($CurlConnect, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($CurlConnect, CURLOPT_USERPWD, $login.':'.$password);
$Result = curl_exec($CurlConnect);
header('Cache-Control: public');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="new.pdf"');
header('Content-Length: '.strlen($Result));
v($Result) ;
文件new.pdf在浏览器中显示,但是坏掉了。
编辑: 我解决了问题。我无法下载 pdf 文件,因为我使用了错误的协议。 这段代码是正确的:
$login = 'LOGIN';
$password = 'PASSOWRD';
$host = "ftp://10.10.10.10/directory/123_ABC.pdf";
$CurlConnect = curl_init();
curl_setopt($CurlConnect, CURLOPT_URL, $host);
curl_setopt($CurlConnect, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($CurlConnect, CURLOPT_USERPWD, $login.':'.$password);
$Result = curl_exec($CurlConnect);
curl_close ($ch);
header('Cache-Control: public');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="new.pdf"');
header('Content-Length: '.strlen($Result));
echo $Result;
【问题讨论】:
-
用什么方法来密码保护这个文件? HTTP 身份验证?
-
将标题数据设置为 application/pdf 并使用 echo file_get_contents();和死();