简体   繁体   English

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

[英]php force download pdf from a dynamic link and store to local folder

I am building a script that imap's into a google mailbox, gets a message, and extracts a link from the body. 我正在构建一个脚本,将imap放到Google邮箱中,获取一条消息,并从正文中提取链接。 The link, when opened via php, starts a download process of a PDF file from an external source. 该链接通过php打开后,将开始从外部源下载PDF文件。

Here is what I need to do: 这是我需要做的:

  1. Programmatically open the link which initiates a download 以编程方式打开启动下载的链接
  2. Download the PDF, bypassing any dialog boxes obviously 下载PDF,绕过任何对话框
  3. Store the PDF file to a local folder on the web server 将PDF文件存储到Web服务器上的本地文件夹中

I have been successful up to the point where I can initiate the download process and download the file. 我已经成功完成了可以启动下载过程和下载文件的工作。 However, every attempt at opening the PDF finds the file to be corrupted and I can't figure out why. 但是,每次打开PDF的尝试都会发现文件已损坏,我不知道为什么。 Here's what I'm trying now. 这就是我现在正在尝试的。 The following is based on similar topics. 以下基于相似的主题。

    $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;

I am thinking that maybe there's an issue with the filename I'm passing? 我在想,我传递的文件名可能存在问题?

Note: This script will be setup as a CRON task that is run on the server every few seconds, in order to constantly fetch from OUR own mailbox, so this is not going to have any type of user interaction or be a security risk to a user. 注意:此脚本将被设置为每隔几秒钟在服务器上运行的CRON任务,以便不断从我们自己的邮箱中提取内容,因此不会有任何类型的用户交互,也不会对用户。

Try: 尝试:

$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;

You cannot use filesize() on remote files. 您不能在远程文件上使用filesize()。 Most likely, the errors from that call are showing up at the beginning of the file and thus corrupting it. 最有可能的是,该调用中的错误显示在文件的开头,从而损坏了文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM