php 下载文件,考虑到各种格式,解决下载时部分出现文件损坏的问题
2016-08-15 14:57
645 查看
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;
}
原文地址:http://www.oschina.net/code/snippet_119226_5070
//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;
}
原文地址:http://www.oschina.net/code/snippet_119226_5070
相关文章推荐
- php提取csv格式文件中的字符串出现的问题及解决办法
- PHP实现多种格式文件下载并解决乱码问题
- php 文件下载 出现下载文件内容乱码损坏的解决方法(推荐)
- 解决IE中部分文件格式不能下载的问题(附MIME大全)
- php 文件下载 出现下载文件内容乱码损坏的解决方法
- PHP文件下载(考虑到各种格式)
- 解决IE中部分文件格式不能下载的问题(附MIME大全
- php 文件下载 出现下载文件内容乱码损坏的解决方法
- php 文件下载 出现下载文件内容乱码损坏的解决方法(推荐)
- 解决http下载部分文件格式(如*.pdb)不能正常下载的问题
- php 文件下载 出现下载文件内容乱码损坏的解决方法
- php 文件下载 出现下载文件内容乱码损坏的解决方法(推荐)
- php中强制下载文件的代码(解决了IE下中文文件名乱码问题)
- PHP解决浏览器 下载文件 中文名称乱码问题--兼容大多浏览器
- php读取csv文件后,uft8 bom导致在页面上显示出现问题的解决方法
- IO 流读取文件时候出现乱码 文件编码格式问题 怎么转换解决方法
- 解决php输出文件下载时文件名含中文时出现乱码
- 在X64 RedHat5 或者 RedHat4 下编译安装PHP出现的各种问题及解决
- 解决IIS出现包含空格的项目的文件/路径部分需要用括号括起来的问题
- 解决Myeclipse中导入epf文件之后xml及jsp等各种页面中的部分显示异常问题