我正在构建一个脚本,imap's到一个谷歌邮箱,得到一个消息,并从正文提取链接。当通过php打开该链接时,将开始从外部源下载PDF文件。
下面是我需要做的:
- 以编程方式打开启动下载的链接
- 下载PDF,绕过任何对话框
- 将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;
我在想,也许有一个问题与我传递的文件名?
注意:这个脚本将被设置为一个CRON任务,每隔几秒钟在服务器上运行一次,以便不断地从我们自己的邮箱中获取,所以这不会有任何类型的用户交互,也不会对用户构成安全风险。
尝试:
$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;
不能在远程文件上使用filesize()。最有可能的是,调用的错误出现在文件的开头,从而破坏了文件。