[請益] php 檔案下載的問題

看板PHP作者 (wade)時間13年前 (2012/04/15 23:33), 編輯推噓4(4012)
留言16則, 6人參與, 最新討論串1/1
各位大大好,小弟想要透過header方式下載上傳server上的檔案,可是不知道為什麼 下載下來的東西都是失敗的,打開都顯示錯誤,但是mp3的檔案又是沒有問題, 不知道各位大大有沒有遇過這樣的問題,我使用xampp1.77架的網站 附上程式碼 都是參考網路上的文章~~$p是檔案的名稱 $len = filesize($p); $filename = basename($p); $file_extension = strtolower(substr(strrchr($filename,"."),1)); 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; //禁止下面幾種類型的檔案被下載 case "php": case "htm": case "html": case "txt": die("Cannot be used for ". $file_extension ." files!"); break; default: $ctype="application/force-download"; } header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header('Cache-Control: private', false); header("Content-Description: File Transfer"); header("Content-Type: $ctype"); $header="Content-Disposition: attachment; filename=".$p.";"; header($header ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".$len); readfile($p); -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.242.0.16

04/15 23:54, , 1F
11622篇你參考看看。
04/15 23:54, 1F

04/16 01:23, , 2F
改成他的寫法還是一樣的結果~"~
04/16 01:23, 2F

04/16 09:52, , 3F
apache有裝deflat或gzip嗎?
04/16 09:52, 3F

04/16 15:01, , 4F
哪邊看有沒有裝呢?? 應該是沒有~
04/16 15:01, 4F

04/16 16:37, , 5F
phpinfo()吧
04/16 16:37, 5F

04/16 23:07, , 6F
我看到我的apaceh裡面有寫HTTP_ACCEPT_ENCODING
04/16 23:07, 6F

04/16 23:07, , 7F
gzip,deflate,sdch 這樣是有裝還是沒裝呢?
04/16 23:07, 7F

04/17 00:55, , 8F
推!用xampp1.7.3測你的程式碼,正常,我也很好奇你的問題
04/17 00:55, 8F

04/19 01:28, , 9F
該不會是環境的問題吧!?? @@
04/19 01:28, 9F

04/20 19:05, , 10F
我最近也碰到此問題,網路上有人說是網頁檔案編碼的問題
04/20 19:05, 10F

04/20 19:05, , 11F
若在windows下,要把 Encode in UTF-8 without BOM改掉
04/20 19:05, 11F

04/20 19:05, , 12F
換成utf8就好>
04/20 19:05, 12F

04/20 19:06, , 13F
但我是linux環境,所以沒辦法測試,不知是否知道解法的@@
04/20 19:06, 13F

04/20 20:00, , 14F
我試出來了,先前的文章也有提到類似的方法
04/20 20:00, 14F

04/20 20:01, , 15F
在程式的最前端加上 ob_end_clean(); 便可下載到正常的檔
04/20 20:01, 15F

04/25 09:25, , 16F
謝謝大大 可以正常使用了 看來得去查一下為什麼~"~
04/25 09:25, 16F
文章代碼(AID): #1FYkfKvO (PHP)
文章代碼(AID): #1FYkfKvO (PHP)