【发布时间】:2013-02-25 20:55:18
【问题描述】:
我想从this link 下载一个 pdf 文件。从页面中选择每周总结,我要下载第一个pdf。
PDF每周总结-上周:按知情人排序
我正在使用以下代码尝试下载pdf,
<?php
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
if (file_exists($file))
{
if (FALSE!== ($handler = fopen($file, 'r')))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: chunked'); //changed to chunked
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
//header('Content-Length: ' . filesize($file)); //Remove
//Send the content in chunks
while(false !== ($chunk = fread($handler,4096)))
{
echo $chunk;
}
}
exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";
?>
我似乎无法使用 PHP 下载。我可以自己下载确认弹出窗口。有什么建议吗?
我也试过curl,还是不能下载。文件大小为零。
<?php
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
$path = 'c:\download\output.pdf';
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
?>
【问题讨论】:
-
为什么不直接指向客户?这不像您实际上正在更改有关 pdf 的任何内容 - 只是代理它。
-
卷曲将是这些天通常的方式
-
@MarcB,你能告诉我怎么做吗?
-
您所做的只是抓取 pdf 文件并将其回显。那么为什么不直接将该网址输入您的浏览器,或者将其作为您网站上的链接提供呢?
-
@MarcB,我不需要在我的网站上显示它。只想下载PDF文件。
标签: php