【问题标题】:correct PHP headers for pdf file download正确的 pdf 文件下载的 PHP 标头
【发布时间】:2013-12-03 12:35:47
【问题描述】:

当用户单击链接时,我真的很难让我的应用程序打开 pdf。

到目前为止,锚标记重定向到发送标题的页面:

$filename='./pdf/jobs/pdffile.pdf;

$url_download = BASE_URL . RELATIVE_PATH . $filename;

header("Content-type:application/pdf");
header("Content-Disposition:inline;filename='$filename");
readfile("downloaded.pdf");

这似乎不起作用,过去有没有人成功解决过这个问题?

【问题讨论】:

  • 错字?尝试添加 ' $filename='./pdf/jobs/pdffile.pdf'; 并在此行中添加 header("Content-Disposition:inline;filename='$filename"); 缺少引号。
  • 您如何/为什么使用$url_download?

标签: php pdf header


【解决方案1】:

w3schools 上的示例 2 显示了您想要实现的目标。

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>

还要记住,

重要的是要注意 header() 必须在任何 发送实际输出(在 PHP 4 及更高版本中,您可以使用 output 缓冲来解决这个问题)

【讨论】:

  • 哈哈是真的。我提到它只是因为它有相同的例子。
  • 输出通知加1。
  • 记得去掉文件名两边的单引号。如果您使用 filename='downloaded.pdf',某些浏览器会尝试使用文件名中的引号保存文件。我最近在 OSX 上遇到过这种情况。
  • readfile() 对大文件非常不利,因为当一些用户要下载时,每个下载文件都会缓冲在服务器内存上!!!
  • 为什么PDF会有HTML标签?
【解决方案2】:
$name = 'file.pdf';
//file_get_contents is standard function
$content = file_get_contents($name);
header('Content-Type: application/pdf');
header('Content-Length: '.strlen( $content ));
header('Content-disposition: inline; filename="' . $name . '"');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
echo $content;

【讨论】:

  • 请注意,将整个文件内容加载到变量中可能会与内存限制发生冲突。这就是为什么readfile() 是首选解决方案的原因。
  • 我在 Edge 浏览器中遇到了无法检测 PDF 的问题,此代码修复了它。谢谢!!!!
【解决方案3】:

在您的代码中需要考虑一些事项。

首先,正确编写这些标题。你永远不会看到任何服务器发送Content-type:application/pdf,标头是Content-Type: application/pdf,有间隔,首字母大写等。

Content-Disposition 中的文件名只是文件名,而不是它的完整路径,虽然我不知道它是否强制,但这个名称包含在 " 而不是 ' 中。另外,您最后的' 丢失了。

Content-Disposition: inline 表示应该显示文件,而不是下载文件。请改用attachment。

此外,将文件扩展名设为大写,以使其与某些移动设备兼容。 (更新:很确定只有黑莓有这个问题,但世界已经从这些问题上转移了,所以这可能不再是一个问题)

话虽如此,您的代码应该看起来更像这样:

<?php

    $filename = './pdf/jobs/pdffile.pdf';

    $fileinfo = pathinfo($filename);
    $sendname = $fileinfo['filename'] . '.' . strtoupper($fileinfo['extension']);

    header('Content-Type: application/pdf');
    header("Content-Disposition: attachment; filename=\"$sendname\"");
    header('Content-Length: ' . filesize($filename));
    readfile($filename);

Content-Length 是可选的,但如果您希望用户能够跟踪下载进度并检测下载是否中断,这也很重要。但是在使用它时,您必须确保不会随文件数据一起发送任何内容。确保&lt;?php 之前或?&gt; 之后绝对没有任何内容,甚至没有空行。

【讨论】:

  • 你不需要结束标签?&gt;。在这种情况下,最好将其删除。
  • 相当肯定,将标题提供的文件名括在单引号中会全面破坏下载,因此正如您所建议的,这会产生很大的不同。
【解决方案4】:

我最近遇到了同样的问题,这对我有帮助:

    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename="FILENAME"'); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize("PATH/TO/FILE")); 
    ob_clean(); 
    flush(); 
    readfile(PATH/TO/FILE);      
    exit();

我找到了这个答案here

【讨论】:

  • ob_start( );在顶部确保数据损坏
  • ob_start() 并没有帮助我,但是当我添加 ob_clean() 和 flush() 时它起作用了!
  • 这是这里唯一完美的解决方案!
  • 这个有效。内容类型:application/octet-stream
【解决方案5】:

你可以试试这个,readfile 需要完整的文件路径。

        $filename='/pdf/jobs/pdffile.pdf';            
        $url_download = BASE_URL . RELATIVE_PATH . $filename;            

        //header("Content-type:application/pdf");   
        header("Content-type: application/octet-stream");                       
        header("Content-Disposition:inline;filename='".basename($filename)."'");            
        header('Content-Length: ' . filesize($filename));
        header("Cache-control: private"); //use this to open files directly                     
        readfile($filename);

【讨论】:

    【解决方案6】:

    你需要定义文件的大小...

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

    而这一行是错误的:

    header("Content-Disposition:inline;filename='$filename");

    你搞砸了配额。

    【讨论】:

    • 不,您不需要提供文件的大小。这也是错误的可能来源。
    • 取决于 PDF 插件版本。给它是安全的。
    • 无法使其正常工作。我正在测试大小的 $file 变量是什么?是文件 URL ./pdf/jobs/pdftitle.pdf 吗?
    • 是的,它是文件位置......也许你搞砸了路径?尝试给出绝对路径。
    【解决方案7】:
    header("Content-type:application/pdf");
    
    // It will be called downloaded.pdf thats mean define file name would be show
    
    header("Content-Disposition:attachment;filename=  $fileName  ");
    
    // The PDF source is in original.pdf
    
    readfile($file_url);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-17
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多