【问题标题】:How to download this PDF file using php如何使用 php 下载此 PDF 文件
【发布时间】: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


【解决方案1】:

最后我可以下载带有 PHP curl 和 CURLOPT_REFERER 设置的 PDF 文件。下面是代码。

<?php

$url  = 'https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA';
$path = "/pdf/output.pdf";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, 'https://www.sedi.ca/sedi/SVTReportsAccessController?menukey=15.03.00&locale=en_CA');

$data = curl_exec($ch);

curl_close($ch);

$result = file_put_contents($path, $data);

if (!$result) {
    echo "error";
} else {
    echo "success";
}
?>

【讨论】:

  • 我只看到“成功”,仅此而已。为什么浏览器不开始下载?
  • 你能解释一下吗:curl_setopt($ch, CURLOPT_REFERER, 'sedi.ca/sedi/…);
【解决方案2】:

与其让您的 PHP 服务器充当代理,不如直接:

<?PHP
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
header("Location: $file");
?>

第 2 版

<?PHP
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA";
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="filename.pdf"');
readfile($file);
?>

版本 3

curl_setopt($ch, CURLOPT_REFERER, 'https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA');

将此行添加到您的原始代码中?

【讨论】:

  • 我只想在本地下载 pdf,但不显示在我的网站上。
  • 看来这个网站不喜欢人们直接下载PDF的我只能猜测你需要设置一个推荐URL或其他东西。 Hotlinking
【解决方案3】:

另外,正如这里提到的:

Download a image from SSL using curl?

对于 https,我们应该添加以下内容:

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

【讨论】:

    猜你喜欢
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多