[請益] php 檔案下載的問題
各位大大好,小弟想要透過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
04/15 23:54, 1F
→
04/16 01:23, , 2F
04/16 01:23, 2F
推
04/16 09:52, , 3F
04/16 09:52, 3F
→
04/16 15:01, , 4F
04/16 15:01, 4F
→
04/16 16:37, , 5F
04/16 16:37, 5F
→
04/16 23:07, , 6F
04/16 23:07, 6F
→
04/16 23:07, , 7F
04/16 23:07, 7F
推
04/17 00:55, , 8F
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
04/20 19:05, 11F
→
04/20 19:05, , 12F
04/20 19:05, 12F
→
04/20 19:06, , 13F
04/20 19:06, 13F
推
04/20 20:00, , 14F
04/20 20:00, 14F
→
04/20 20:01, , 15F
04/20 20:01, 15F
→
04/25 09:25, , 16F
04/25 09:25, 16F
PHP 近期熱門文章
PTT數位生活區 即時熱門文章