【问题标题】:Download file corrupt. Content-type not working?下载文件损坏。内容类型不起作用?
【发布时间】:2011-08-21 10:51:16
【问题描述】:

我想允许用户下载 pdf 文件,下载代码如下....出于某种奇怪的原因,即使正在下载文件,我也收到一条错误消息,指出服务器上的文件已损坏。 ..有人可以帮助我并指出我在哪里犯了错误。

<php

$name = $_POST["name_first"];

    $mail = $_POST['email'];


    $number = $_POST['phone_number'];

            $email_message = "first name: {$name} email is {$mail} number is {$number} ";
            mail('fanaa@gmail.com', 'Form Response', $email_message);

                if ($mail == "" OR $name == "" OR $number == "")
                    {
                        echo "Enter valid details  ";

                    }
                    else
                    {
                        header('Content-type: application/pdf');
                        header('Content-Disposition: attachment; filename="tokina.pdf"');
                        readfile('docs/tokina.pdf');

                    }
?>

【问题讨论】:

  • 您可以将原始文件与下载的文件进行二进制比较,看看是否能找出问题所在。如果差异仅在前 2 个字节或后 2 个字节,我可能会提供帮助。
  • 在执行的脚本中是否有其他输出,例如空格、echo 或其他什么?
  • 如果你的pdf文件没问题,也许你可以使用图书馆的电子邮件,例如swiftmailer.org
  • @KingCrunch 是的,这段代码实际上是 if 语句的一部分....我在您的查询后编辑了我的问题,请查看它
  • 我怀疑存在 标签关闭问题。如果在脚本的开头或结尾有空格,在 标记之外,它们将被输出,导致数据损坏。或者,可能是内容长度问题:您是否尝试过添加如下行? header("内容长度:".filesise("tokina.pdf"));

标签: php download content-type


【解决方案1】:

我使用此代码下载 pdf:

header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header('Content-Type: application/octetstream');
    header("Content-Transfer-Encoding: Binary");
    header("Content-length: ".filesize($file));
    header("Content-disposition: attachment; filename=\"".basename($filename)."\"");
    readfile("$file");                
  }

这应该没问题,并确保没有空格或返回字符(完全不要转义php是最好的解决方案)。

如果发现还是有问题,用记事本打开损坏的文件(里面可能有php错误警告)。

希望这会有所帮助!

【讨论】:

  • 你能告诉我 $file 是什么意思吗?我没有得到那部分
  • $file 是文件路径,即 ../myfiles/thisone/example.pdf,在你的情况下你可以把 $file = 'docs/tokina.pdf'
  • Abe,如果你写readfile($file); 不带引号可能会更清楚。
  • "Content-Type: application/octetstream" 标头在我的案例中。谢谢
  • 在这种情况下使用记事本打开 pdf 文件是一个很好的建议。谢谢它对我有很大帮助
【解决方案2】:

删除标题并查看页面,您是否看到任何错误消息?如果 PHP 输出的不是实际的 PDF 源文件,则该文件似乎已损坏。

【讨论】:

  • 如果我删除第一行的标题...它的输出为 tokina.pdf.htm
  • 如果我删除了两个标题,那么 PHP 会显示一个带有二进制编码行的页面
  • 很明显,但我没想到这一点,这有帮助,谢谢!
【解决方案3】:
header('Content-type: application/pdf');

启用 PHP 扩展 php_gettext 就完成了。

【讨论】:

    【解决方案4】:

    尝试去掉

    中的双引号
    header('Content-type: "application/octet-stream"');
    

    原来如此

    header('Content-type: application/octet-stream');
    

    【讨论】:

    • 我认为 PDF 的内容类型应该是 application/pdf。见php.net/manual/en/function.header.php
    • 我首先尝试了应用程序/pdf。我得到了同样的错误,经过一些研究,我阅读了有关 application/octet-stream 并将其集成到代码中
    【解决方案5】:

    也许您的内容类型不正确。试试这个:

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

    【讨论】:

      【解决方案6】:

      您的 PDF 文件 tokina.pdf 未上传或与 PHP 文件不在同一目录中。这就是它保存为“tokina.pdf.htm”的原因——它正在加载 404 页面的 HTML。这就是您的浏览器/PDF 查看器认为该文件“已损坏”的原因——因为它的扩展名是 PDF,但其内容不是。

      确保文件已上传,如果已上传,请确保 readfile 指向正确的路径。如果不在同一个文件夹中,请使用相对/绝对路径,例如:

      readfile('docs/tokina.pdf');
      

      是的,内容类型应该是application/pdf

      【讨论】:

      • 我正在使用 WAMP 服务器来测试我的代码...并且 PDF 文件的位置在 WAMP 的“WWW”目录中
      【解决方案7】:

      使用这个脚本

         header('Content-Type: application/force-download');
         header('Content-Disposition: attachment; filename='.$filename);
         header('Content-Transfer-Encoding: binary');
         header('Content-Length: '.filesize($filenamepath));
      
         readfile($filenamepath);
      

      我遇到了同样的问题。用UltraEdit之类的十六进制编辑器对比原始文件和下载的文件,发现损坏文件的开头有一些字符。

      问题是在?&gt;标记PHP代码结束后,我的代码中有好几次行终止符。

      删除?&gt; 之后的所有行终止符并阅读论坛文章Downloaded Files are corrupt - Common Problem。这对我有用。

      希望对你有帮助。

      【讨论】:

        【解决方案8】:

        我用

        $download_path = your path (where to look for the files)
        set_time_limit(0);
        $file_url = $download_path . $data['link'];
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . basename($file_url). '"');
        //then to read the file
        readfile($file_url);
        

        这通常对我有用

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-12
          • 1970-01-01
          • 1970-01-01
          • 2015-07-14
          • 2015-07-16
          • 2017-12-04
          • 2019-01-28
          • 1970-01-01
          相关资源
          最近更新 更多