簡體   English   中英

下載文件損壞。內容類型不起作用?

[英]Download file corrupt. Content-type not working?

我想允許用戶下載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');

                    }
?>

我用這段代碼下載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");                
  }

這應該沒問題,並確保沒有空格或返回字符(不要逃避php是最好的解決方案)。

如果您發現仍有問題,請用記事本打開損壞的文件(里面可能有一個php錯誤警告)。

希望這可以幫助!

刪除標題並查看頁面,您是否看到任何錯誤消息? 如果PHP輸出的內容不是實際的PDF源,則該文件似乎已損壞。

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

啟用PHP擴展php_gettext ,你就完成了。

使用此腳本

   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這樣的十六進制編輯器比較原始文件和下載的文件,我在損壞的文件的開頭找到了一些字符。

問題是在?>標記PHP代碼結束后,在我的代碼中有幾次行終止符。

?>之后刪除所有行終止符,並閱讀論壇文章Downloaded Files are corrupt - 常見問題 這對我有用。

我希望能幫助你。

我用

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

這通常對我有用

嘗試取出雙引號

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

所以它變成了

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

也許你的內容類型不正確。 試試這個:

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

您的PDF文件tokina.pdf要么未上傳,要么與PHP文件不在同一目錄中。 這就是為什么它保存為“tokina.pdf.htm” - 它正在為404頁面加載HTML。 這就是為什么您的瀏覽器/ PDF查看器認為該文件“已損壞” - 因為它的擴展名是PDF但其內容不是。

確保文件已上載,如果是,請確保readfile指向正確的路徑。 如果它不在同一文件夾中,請使用相對/絕對路徑,例如:

readfile('docs/tokina.pdf');

是的,內容類型應該是application/pdf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM