简体   繁体   English

PDF下载无法正常运行

[英]PDF Downloading not functioning properly

I am using the following code to download a PDF.... 我正在使用以下代码下载PDF。...

   header('Content-Type: application/pdf');
   header('Content-Disposition: attachment; filename="NewName.pdf"');
   readfile($root_path.'/full-tile-book.pdf');

It's a 13 MB file and only downloads like 130k and of course can't be opened. 它是一个13 MB的文件,只能下载130k,当然无法打开。 How do I debug what the issue is here? 如何调试这里的问题? My path to the pdf is correct. 我的pdf路径正确。

Perhaps you are running into a memory/filesize error? 也许您遇到了内存/文件大小错误? I've had problems in the past dumping large files with readfile. 过去使用readfile转储大文件时遇到了问题。 In addition to setting the Content-Length header, I recommend using fpassthru() , as it does not read the file into a buffer, it just dumps it. 除了设置Content-Length标头外,我建议使用fpassthru() ,因为它不会将文件读入缓冲区,而只是将其转储。

set_time_limit(0); // disable timeout
$file = $root_path.'/full-tile-book.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="NewName.pdf"');
header('Content-Length: ' . filesize($file));
session_write_close(); // remove this line if sessions are not active
$f = fopen($file, 'rb');
fpassthru($f);
fclose($f);
exit;

EDIT: If you are using any sessions_code, it is a good idea to end the session before starting the file dump process. 编辑:如果您使用任何session_code,一个好主意是在开始文件转储过程之前结束会话。 I have updated my example to reflect this. 我已经更新了我的示例以反映这一点。

I'd add a few items. 我要添加一些项目。 Assign the filename to $file, then try: 将文件名分配给$ file,然后尝试:

header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);

This will ensure that the client is expecting the correct length, and the output buffer flushes nicely in between. 这将确保客户端期望正确的长度,并且输出缓冲区在两者之间很好地刷新。

see downloaded file perhaps have bad address use in PHP. 看到下载的文件在PHP中可能使用了错误的地址。
You can follow this tutorial for file download in PHP. 您可以按照本教程的说明在PHP中下载文件。

HTML headers must be sent before any output is sent to the browser. 必须先发送HTML标头,然后才能将任何输出发送到浏览器。 PHP uses the header function to pass raw HTML headers. PHP使用标头函数传递原始HTML标头。 For this example we're going to get the filename from the URL www.yourdomain.com/download.php?file=download.zip. 对于此示例,我们将从URL www.yourdomain.com/download.php?file=download.zip获取文件名。

<? 
$dir="/path/to/file/";
if (isset($_REQUEST["file"])) {
    $file=$dir.$_REQUEST["file"];
    header("Content-type: application/force-download");
    header("Content-Transfer-Encoding: Binary");
    header("Content-length: ".filesize($file));
    header("Content-disposition: attachment; filename="".basename($file).""");
    readfile("$file");
} else {
    echo "No file selected";
}
 ?>

We started with setting the directory where the files to be downloaded are located in $dir. 我们首先在$ dir中设置要下载文件的目录。 Be sure not to use in $dir. 确保不要在$ dir中使用。 Then we checked to make sure a filename was specified in the request. 然后,我们检查以确保在请求中指定了文件名。 If a file was specified then we set $file to the path to the file and the filename. 如果指定了文件,则将$ file设置为文件和文件名的路径。 Now that the prep work is done its time to send the file to the browser. 现在准备工作已经完成,可以将文件发送到浏览器了。

The first header statement tells the browser to expect a download. 第一个标头语句告诉浏览器进行下载。 The next two header statements tell the browser the format of the data and the size of the file respectively. 接下来的两个标题语句分别告诉浏览器数据的格式和文件的大小。 The last header statement tells the browser the name of the file. 最后一个标头语句告诉浏览器文件名。 Finally the readfile statement sends the file to the browser. 最后,readfile语句将文件发送到浏览器。

More information. 更多信息。

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

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