PHP如何编写适用于各种文件的通用下载方法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计104个文字,预计阅读时间需要1分钟。
phpfunction download_file($file) { if (is_file($file)) { $length=filesize($file); $type=mime_content_type($file); $showname=ltrim(strrchr($file, '/'), '/'); header('Content-Description: File Transfer'); header('Content-type: ' . $type); header('Content-Length: ' . $length); header('Content-Disposition: attachment; filename=' . $showname . ''); readfile($file); exit; }}
function download_file($file){ if(is_file($file)){ $length = filesize($file); $type = mime_content_type($file); $showname = ltrim(strrchr($file,'/'),'/'); header("Content-Description: File Transfer"); header('Content-type: ' . $type); header('Content-Length:' . $length); if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"'); } else { header('Content-Disposition: attachment; filename="' . $showname . '"'); } readfile($file); exit; } else { exit('文件已被删除!'); } }
本文共计104个文字,预计阅读时间需要1分钟。
phpfunction download_file($file) { if (is_file($file)) { $length=filesize($file); $type=mime_content_type($file); $showname=ltrim(strrchr($file, '/'), '/'); header('Content-Description: File Transfer'); header('Content-type: ' . $type); header('Content-Length: ' . $length); header('Content-Disposition: attachment; filename=' . $showname . ''); readfile($file); exit; }}
function download_file($file){ if(is_file($file)){ $length = filesize($file); $type = mime_content_type($file); $showname = ltrim(strrchr($file,'/'),'/'); header("Content-Description: File Transfer"); header('Content-type: ' . $type); header('Content-Length:' . $length); if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"'); } else { header('Content-Disposition: attachment; filename="' . $showname . '"'); } readfile($file); exit; } else { exit('文件已被删除!'); } }

