您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

任意格式文件下载

2026/1/1 13:50:14发布18次查看
支持任意格式的文件下载
函数有两个参数,第一个参数是文件在服务器中完成路径,第二个参数是下载显示文件名称。
/** * 下载文件 * filename 不包括后缀名 */ public function download($_path, $filename = '') { if (file_exists($_path)) { $fullpath = chtml::decode($_path); $filename = $filename ? $filename : substr(strrchr($fullpath, '/'), 1); // parse info / get extension $fsize = filesize($fullpath); $path_parts = pathinfo($fullpath); $ext = strtolower($path_parts[extension]); $filename .= '.' . $ext; // determine content type switch ($ext) { case 'apk': $ctype = 'application/vnd.android.package-archive'; break; case 'chm': $ctype = 'application/octet-stream'; break; case pdf: $ctype = application/pdf; break; case txt: $ctype = application/txt; 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; default: $ctype = application/force-download; } $ua = $_server[http_user_agent]; $encoded_filename = rawurlencode($filename); $encoded_filename = str_replace(+, %20, $encoded_filename); header(pragma: public); // required header(expires: 0); header(cache-control: must-revalidate, post-check=0, pre-check=0); header(cache-control: private, false); // required for certain browsers header(content-type: $ctype); // header('content-disposition: attachment; filename='.rawurlencode($filename).''); if (preg_match(/msie/, $ua)) { header('content-disposition: attachment; filename=' . $encoded_filename . ''); } else if (preg_match(/firefox/, $ua)) { header(content-disposition: attachment; filename*=utf8'' . $filename . ''); } else { header('content-disposition: attachment; filename=' . $filename . ''); } header(content-transfer-encoding: binary); header(content-length: . $fsize); ob_clean(); flush(); readfile($fullpath); } else { throw new exception('文件不存在!', 1); } }
复制代码
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product