2016-05-13 69 views
3

我有一個php代碼,用於根據URL中的file_id下載文件。所有的作品很好,但我有擴展名下載文件的問題.docphp readfile壞字符集

這是我的代碼,什麼即時通訊做錯了?

$ file = mysql_fetch_array(mysql_query(「SELECT * FROM」.sqlprefix。「files WHERE id ='」。$ _ GET ['id']。「'」)); $ tmp = explode(「。」,$ file ['url']); $ tmp = $ tmp [count($ tmp)-1]; // $ tmp =「doc」;

switch ($tmp) { 
    case "pdf": $ctype="application/pdf"; break; 
    case "exe": $ctype="application/octet-stream"; break; 
    case "zip": $ctype="application/zip"; break; 
    case "docx": $ctype="application/vnd.openxmlformats-officedocument.wordprocessingml.document"; break; 
    case "doc": $ctype="application/msword"; break; 
    case "csv": 
    case "xls": 
    case "xlsx": $ctype="application/vnd.ms-excel"; break; 
    case "ppt": $ctype="application/vnd.ms-powerpoint"; break; 
    case "gif": $ctype="image/gif"; break; 
    case "png": $ctype="image/png"; break; 
    case "jpeg": 
    case "jpg": $ctype="image/jpg"; break; 
    case "tif": 
    case "tiff": $ctype="image/tiff"; break; 
    case "psd": $ctype="image/psd"; break; 
    case "bmp": $ctype="image/bmp"; break; 
    case "ico": $ctype="image/vnd.microsoft.icon"; break; 
    default: $ctype="application/force-download"; 
} 

$baseName = basename($file['url_del']); 
$fileSize = filesize($file['url_del']); 
$url = $file['url_del']; 

// $ctype = "application/msword"; 
// $baseName = "File name with spaces and diacritic ěščěšč.doc" 
// $fileSize = "214016" 
// $url = "./files/File name with spaces and diacritic ěščěšč.doc"; 


    header('Content-Description: File Transfer'); 
    header('Content-Type: '.$ctype); 
    header('Content-Disposition: attachment; filename='.$baseName); 
    header('Expires: 0'); 
    header('Pragma: public'); 
    header('Content-Length: '.$fileSize); 
    readfile($url); 
    exit; 

下載文件後,我得到這個。

enter image description here

我認爲,這個問題必須在編碼......在這裏輸入的形象描述

回答

0

這是可怕的對我來說太。 我發現這裏的slution:PHP readfile() causing corrupt file downloads

嘗試把ReadFile的在此之前線():

//clean all levels of output buffering 
while (ob_get_level()) { 
    ob_end_clean(); 
}