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

5 php content-type download

我想允许用户下载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');

                    }
?>
Run Code Online (Sandbox Code Playgroud)

Abe*_*llo 13

我用这段代码下载pdfs:

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");                
  }
Run Code Online (Sandbox Code Playgroud)

这应该没问题,并确保没有空格或返回字符(不要逃避php是最好的解决方案).

如果您发现仍有问题,请用记事本打开损坏的文件(里面可能有一个php错误警告).

希望这可以帮助!