php force从动态链接下载pdf并存储到本地文件夹

Jar*_*ier 1 php pdf download mime-types

我正在构建一个脚本,将imap放入谷歌邮箱,获取消息,并从正文中提取链接.该链接在通过php打开时,会从外部源开始下载PDF文件.

这是我需要做的:

  1. 以编程方式打开启动下载的链接
  2. 下载PDF,明显绕过任何对话框
  3. 将PDF文件存储到Web服务器上的本地文件夹

我已经成功了,我可以启动下载过程并下载文件.但是,每次打开PDF的尝试都会发现文件已损坏,我无法弄清楚原因.这就是我现在正在尝试的.以下是基于类似的主题.

    $filename =  http://cckk.ca/KE645R26/geico.com_SEO_Domain_Dashboard_20121101_00.pdf

    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($filename));
    ob_clean();
    flush();
    readfile($filename);
    exit;
Run Code Online (Sandbox Code Playgroud)

我在想,也许我传递的文件名存在问题?

注意:此脚本将被设置为每隔几秒在服务器上运行的CRON任务,以便不断从我们自己的邮箱中获取,因此这不会有任何类型的用户交互或对安全性造成安全风险.用户.

iam*_*eed 5

尝试:

$filename = 'http://cckk.ca/KE645R26/geico.com_SEO_Domain_Dashboard_20121101_00.pdf';

file_put_contents(
    '/your/server/path/folder/' . basename($filename), // where to save file
    file_get_contents($filename)
);

exit;
Run Code Online (Sandbox Code Playgroud)