EcStart blog << 詳情請入內
返回列表 發帖

header檔案下載錯誤

請教各位大大,我寫一個檔案下載的程式使用header函數
在xp的電腦上執行都可以正常,下載的檔案都可以正常執行
但同樣的程式在windows server 2003上面,一樣可以下載
但沒有一個檔案是好的= =

程式部份

header("Content-type: application/x-download");
header('Content-Disposition: attachment; filename='.$file_name);
header("Content-Transfer-Encoding: binary");
readfile($file_path.$file_name);

ps:xp跟2003server 都使用相同的 Appserv 2.5.8

php.ini設定檔也都一模一樣

但在server 2003 載下來的檔案沒有一個是正確的(損毀檔案)

但直接從資料夾開啟檔案卻是正常

用這帖藥試試看

function dl_file($file){

   //First, see if the file exists
   if (!is_file($file)) { die("<b>404 File not found!</b>"); }

   //Gather relevent info about file
   $len = filesize($file);
   $filename = basename($file);
   $file_extension = strtolower(substr(strrchr($filename,"."),1));

   //This will set the Content-Type to the appropriate setting for the file
  switch( $file_extension ) {
     case "pdf": $ctype="application/pdf"; break;
     case "exe": $ctype="application/octet-stream"; break;
     case "zip": $ctype="application/zip"; break;
     case "doc": $ctype="application/msword"; break;
     case "xls": $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 "mp3": $ctype="audio/mpeg"; break;
     case "wav": $ctype="audio/x-wav"; break;
     case "mpeg":
     case "mpg":
     case "mpe": $ctype="video/mpeg"; break;
     case "mov": $ctype="video/quicktime"; break;
     case "avi": $ctype="video/x-msvideo"; break;

     //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
     case "php":
     case "htm":
     case "html":
     case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;

     default: $ctype="application/force-download";
   }

   //Begin writing headers
   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");
   
   //Use the switch-generated Content-Type
   header("Content-Type: $ctype");

   //Force the download
   $header="Content-Disposition: attachment; filename=".$filename.";";
   header($header );
   header("Content-Transfer-Encoding: binary");
   header("Content-Length: ".$len);
   @readfile($file);
   exit;
}

TOP

感謝大大的回覆,但這支程式我執行要下載的檔案,是直接在網頁上開啟...
也就是整頁亂碼~"~

TOP

我都是用這支 做下載的功能  都還沒遇到過問題  
換個平台  用 Xampp 試試看吧

TOP

感謝大大的回復,我後來試了,問題還是一樣。
如果是word(*.doc)的檔案載下來會都亂碼= =

真的很奇怪
同樣的程式在xp下運行卻是ok的~"~

還是server 2003 需要再設定那些地方呢

TOP

大大我找到問題的來源
我測試上傳utf-8的檔案下載可以正常
一般的檔案下載就都會錯誤
.rar.doc等
還有什麼其他方法嗎

TOP

回覆大大,小弟的問題已解決了...
後來發現原因是有引用到含有BOM的文件,造成下載的編碼錯諤

總算是解決了這個擾人問題了
也感謝大大的回覆
另外有關Xampp的問題
我有試用...但覺得效能好差..

TOP

返回列表