php 下载文件损坏。内容类型不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5933229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:56:57  来源:igfitidea点击:

Download file corrupt. Content-type not working?

phpdownloadcontent-type

提问by Abe Petrillo

I want to allow a user to download a pdf file, the download code is below....for some odd reason even though the file is being downloaded I get an error saying that the file has been damaged on the server...Could someone help me and point out where I am making my mistake.

我想允许用户下载 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('[email protected]', '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');

                    }
?>

回答by Abe Petrillo

I used this code to download pdfs:

我使用此代码下载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");                
  }

This should be fine, and make sure there are no spaces or return characters (don't escape php at all is the best solution).

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

If you find your still having problems, open the corrupted file with notepad (there may be a php error warning inside).

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

Hope this helps!

希望这可以帮助!

回答by Tatu Ulmanen

Remove the headers and look at the page, do you see any error messages? If PHP outputs anything else than the actual PDF source, the file will appear to be corrupted.

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

回答by Imran

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

enable PHP extension php_gettextand you are done.

启用 PHP 扩展php_gettext,你就完成了。

回答by jagumon

Using this script

使用这个脚本

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

   readfile($filenamepath);

I had the same problem. Comparing the original file and the downloaded file with a hexadecimal editor like UltraEdit, I found some characters at the beginning of the corrupted file.

我有同样的问题。用UltraEdit等16进制编辑器对比原始文件和下载的文件,发现损坏文件的开头有一些字符。

The problem was that after ?>marking end of PHP code there were line terminators several times in my code.

问题是在?>标记 PHP 代码结束后,我的代码中出现了多次行终止符。

Remove all the line terminators after ?>and read also the forum article Downloaded Files are corrupt - Common Problem. That worked for me.

之后删除所有行终止符?>并阅读论坛文章下载的文件已损坏 - 常见问题。那对我有用。

I hope that can help you.

我希望这可以帮助你。

回答by Saurav Khatiwada

I use

我用

$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);

this usually works for me

这通常对我有用

回答by Flask

Maybe your content-type is not correct. try this one:

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

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

回答by andrewtweber

Your PDF file tokina.pdf is either not uploaded or not in the same directory as the PHP file. That's why it's saving as "tokina.pdf.htm" - it's loading the HTML for a 404 page instead. That is why your browser/PDF viewer thinks the file is "corrupted" - because its extension is PDF but its contents are not.

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

Make sure the file is uploaded, and if it is, make sure readfileis pointing to the correct path. If it's not in the same folder, use a relative/absolute path, for example:

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

readfile('docs/tokina.pdf');

And yes, the content type should be application/pdf

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

回答by boug

try taking out the double quotes in

尝试取出双引号

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

so it becomes

所以它变成

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