2011-12-08 51 views
0
if($_POST['mode']=="save") { 
$root = $_SERVER['DOCUMENT_ROOT']; 
$path = "/mcboeking/"; 
$path = $root.$path; 
$file_path = $_POST['path']; 
$file = $path.$file_path; 

if(!file_exists($file)) { 
    die('file not found'); 
} else { 
    header("Cache-Control: public"); 
    header("Content-Description: File Transfer"); 
    header('Content-Type: application/force-download'); 
    header("Content-Disposition: attachment; filename=\"".basename($file)."\";"); 
    header("Content-Length: ".filesize($file)); 
    readfile($file);}} 

只要我下載的文件,並打開它,我得到一個錯誤信息。當我嘗試打開.doc文件時,我收到消息:文件結構無效。 而當我嘗試打開一個.jpg文件時:此文件無法打開。它可能已損壞或者Preview無法識別的文件格式。強制下載文件Broken Headers錯誤?

但是當我下載PDF文件時,它們沒有任何問題打開。

有人可以幫助我嗎?

P.s.我嘗試了不同的標題,包括: header('Content-Type:application/octet-stream');

回答

0
 
$fsize = filesize($yourFilePath); 
$allowed_ext = array (

    // archives 
    'zip' => 'application/zip', 

    // documents 
    'pdf' => 'application/pdf', 
    'doc' => 'application/msword', 
    'xls' => 'application/vnd.ms-excel', 
    'ppt' => 'application/vnd.ms-powerpoint', 

); 
$mtype = mime_content_type($file_path); 
if ($mtype == '') { 
    $mtype = "application/force-download"; 
} 
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: $mtype"); 
header("Content-Disposition: attachment; filename=\"$asfname\""); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: " . $fsize); 
//then ur rest code 
0

您的標題對實際內容沒有影響,所以問題是實際內容有問題。

此外,你真的不應該嘗試通過更改Content-Type標題強制下載。離開它應該是什麼。讓客戶決定如何處理它。

0

確保你不輸出任何東西,但readfile()數據:確保PHP代碼<?開始在PHP文件的第一個符號,不存在符號在文件的結尾處的右?>後,如空格或換行符(或者完全刪除?>)。

0

在NOTEPAD中打開PHP文件。按「另存爲」並選擇「ASCI」文件格式。這有助於我的情況,因爲文件是UTF8格式,這是原因。