【发布时间】:2014-11-11 11:27:35
【问题描述】:
我无法设置正确的标题以从 URL 下载文件。 下面的代码只是在浏览器中显示 URL 的内容,而不是下载。
$file_name = "image1.jpg";
$content_type = "image/jpeg";
$url = "http://ctan.imsc.res.in/macros/latex/contrib/incgraph/example.jpg";
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
header('Cache-Control: private',false);
header("Content-type: ".$content_type);
header("Cache-Control: no-store, no-cache");
header("Content-disposition: attachment; filename=".$file_name);
header("Location: ".$url);
exit(0);
提前致谢。
【问题讨论】:
-
你需要获取
example.jpg的内容并通过浏览器。Location:标头只会重定向用户。 -
感谢@BenM 的回复。但我不必在我的服务器中下载图像。
-
@GuruGGulab — 你错了。如果您想向其中添加自己的 HTTP 标头,您可以这样做。
-
我需要直接从 URL 下载文件,而不是在浏览器中显示。如果我的代码有误,请建议我另一种解决方案。我的要求是允许用户下载外部 URL 文件而不是在浏览器中显示。
标签: php attachment force-download