gpt4 book ai didi

php - 如何通过 PHP 正确发送 PDF 文件?

转载 作者:行者123 更新时间:2023-12-02 22:15:51 26 4
gpt4 key购买 nike

我正在尝试发送 PDF 文件。这是正在执行的代码:

$file_path = '/path/to/file/filename.pdf'
$file_name = 'filename.pdf'

header("X-Sendfile: $file_path");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename='$filename'");
readfile($file_path):

每当我直接下载文件时,都很好。但是,当我尝试通过此脚本下载文件时,下载的文件无法打开。我的 pdf 阅读器告诉我它无法打开“文本/纯文本”类型的文件。我也尝试将 Content-type 设置为 application/pdf ,但我得到了同样的错误。我在这里做错了什么?

最佳答案

你试过吗?

$file = '/path/to/file/filename.pdf';
header('Content-Disposition: attachment; filename="'. basename($file) . '"');
header('Content-Length: ' . filesize($file));
readfile($file);

使用 readfile()还消除了您可能遇到的任何内存问题。

关于php - 如何通过 PHP 正确发送 PDF 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14484072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com