douyouzheng2209 2017-06-12 06:49
浏览 119

PDF-使用PHP下载 - > ERR_CONTENT_LENGTH_MISMATCH

I would like to download a PDF via PHP from my server, which is also uploaded via PDF. Although it works with some files I get the following error message with individual files:

ERR_CONTENT_LENGTH_MISMATCH

This is my download function:

if($_GET['funktion'] == 'downloadallgemein')
 {

        $filename = basename($dateiname);
        $path = '/kunden/261105_71522/webseiten/dev.delst/uploads/'.$filename.''; // the file made available for download via this PHP file
        if (file_exists($path)) {
                $mm_type="application/octet-stream"; // modify accordingly to the file type of $path, but in most cases no need to do so

                header("Pragma: public");
                header("Expires: 0");
                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                header("Cache-Control: public");
                header("Content-Description: File Transfer");
                header("Content-Type: " . $mm_type);
                header("Content-Length: " .(string)(filesize($path)) );
                header('Content-Disposition: attachment; filename="'.basename($path).'"');
                header("Content-Transfer-Encoding: binary
");

                readfile($path); // outputs the content of the file

                exit();
        }

        else
        echo'Datei wurde nicht auf dem Server gefunden';
 }  

If I remove header("Content-Length: " .(string)(filesize($path)) ); the download works, but the PDF file is then broken and can not be opened.

The path is correct and the file is located on the server. So the upload works..

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • drfm55597 2017-06-12 07:42
    关注

    header('Content-Type: application/pdf');

    header('Content-Disposition: attachment; filename="' . basename($filename) . '"');

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

    readfile($filename);

    I used this headers for downloading pdf in my php file

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配