【问题标题】:PHP Download file with headerPHP 下载带有标题的文件
【发布时间】:2013-09-18 13:48:00
【问题描述】:

我知道这很容易,但我似乎找不到解决方案。 我想从文件夹中下载 pdf。 pdf 位于名为 forms 的文件夹中。 我的脚本位于不同的文件夹中。

当我尝试使用以下代码下载文件时:

header('Content-Disposition: attachment; filename="forms/form1976.pdf"');

文件名变为:forms-form1976.pdf。不对,文件名应该是:form1976.pdf。如何先输入正确的文件夹?

【问题讨论】:

  • 下面是对 JTC 答案的解释。 header('Content-Disposition: attachment; filename=example.pdf'); 是要“另存为”的文件的名称,并且可以是任何其他名称(具有相应的扩展名)。但是,readfile("/path/to/yourfile.pdf"); 是服务器本身的实际文件。只是一个快速的“仅供参考”;-)

标签: php file header download


【解决方案1】:

你应该这样做

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename=example.pdf');
readfile("/path/to/yourfile.pdf");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    相关资源
    最近更新 更多