下载文件总结

举报
lxw1844912514 发表于 2022/03/27 23:45:41 2022/03/27
【摘要】 生成 HTTP 头强制下载数据到客户端,这在实现文件下载时很有用。 首先感谢@yangweijie的作品,在此基础上衍生出不需要开启php_fileinfo.dll扩展的方法。 方法一:开启扩展情况下 //$file 文件路径public function downloa...


生成 HTTP 头强制下载数据到客户端,这在实现文件下载时很有用。
首先感谢@yangweijie的作品,在此基础上衍生出不需要开启php_fileinfo.dll扩展的方法。
方法一:开启扩展情况下
  1. //$file 文件路径
  2. public function download_file($file){
  3.         if(is_file($file)){
  4.             $length = filesize($file);
  5.             $type = mime_content_type($file);
  6.             $showname =  ltrim(strrchr($file,'/'),'/');
  7.             header("Content-Description: File Transfer");
  8.             header('Content-type: ' . $type);
  9.             header('Content-Length:' . $length);
  10.             if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
  11.                 header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
  12.             } else {
  13.                 header('Content-Disposition: attachment; filename="' . $showname . '"');
  14.             }
  15.             readfile($file);
  16.         } else {
  17.             $this->error('源文件不存在!');
  18.         }
  19.     }
复制代码
方法二,如果没有开启php_fileinfo.dll,谁让咱是没有服务器权限的程序员呢,又不能让人家看不起,唉!可能你会懂。
  1. public function force_download($filename)
  2.     {
  3.         if ($filename == ''){
  4.             return FALSE;
  5.         }
  6.         if (FALSE === strpos($filename, '.')){
  7.             return FALSE;
  8.         }
  9.         $x = explode('.', $filename);
  10.         $extension = end($x);
  11.         $mimes =$this->getMimes();

  12.         // Set a default mime if we can't find it
  13.         if ( ! isset($mimes[$extension])){
  14.             $mime = 'application/octet-stream';
  15.         }else{
  16.             $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  17.         }
  18.         // Generate the server headers
  19.         if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)
  20.         {
  21.             header('Content-Type: "'.$mime.'"');
  22.             header('Content-Disposition: attachment; filename="'.$filename.'"');
  23.             header('Expires: 0');
  24.             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  25.             header("Content-Transfer-Encoding: binary");
  26.             header('Pragma: public');
  27.             header("Content-Length: ".filesize($filename));
  28.         }
  29.         else
  30.         {
  31.             header('Content-Type: "'.$mime.'"');
  32.             header('Content-Disposition: attachment; filename="'.$filename.'"');
  33.             header("Content-Transfer-Encoding: binary");
  34.             header('Expires: 0');
  35.             header('Pragma: no-cache');
  36.             header("Content-Length: ".filesize($filename));
  37.         }
  38.         readfile($filename);
  39.     }
  40. private function getMimes(){
  41.         return $mimes = array(    'hqx'    =>    'application/mac-binhex40',
  42.             'cpt'    =>    'application/mac-compactpro',
  43.             'csv'    =>    array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),
  44.             'bin'    =>    'application/macbinary',
  45.             'dms'    =>    'application/octet-stream',
  46.             'lha'    =>    'application/octet-stream',
  47.             'lzh'    =>    'application/octet-stream',
  48.             'exe'    =>    array('application/octet-stream', 'application/x-msdownload'),
  49.             'class'    =>    'application/octet-stream',
  50.             'psd'    =>    'application/x-photoshop',
  51.             'so'    =>    'application/octet-stream',
  52.             'sea'    =>    'application/octet-stream',
  53.             'dll'    =>    'application/octet-stream',
  54.             'oda'    =>    'application/oda',
  55.             'pdf'    =>    array('application/pdf', 'application/x-download'),
  56.             'ai'    =>    'application/postscript',
  57.             'eps'    =>    'application/postscript',
  58.             'ps'    =>    'application/postscript',
  59.             'smi'    =>    'application/smil',
  60.             'smil'    =>    'application/smil',
  61.             'mif'    =>    'application/vnd.mif',
  62.             'xls'    =>    array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),
  63.             'ppt'    =>    array('application/powerpoint', 'application/vnd.ms-powerpoint'),
  64.             'wbxml'    =>    'application/wbxml',
  65.             'wmlc'    =>    'application/wmlc',
  66.             'dcr'    =>    'application/x-director',
  67.             'dir'    =>    'application/x-director',
  68.             'dxr'    =>    'application/x-director',
  69.             'dvi'    =>    'application/x-dvi',
  70.             'gtar'    =>    'application/x-gtar',
  71.             'gz'    =>    'application/x-gzip',
  72.             'php'    =>    'application/x-httpd-php',
  73.             'php4'    =>    'application/x-httpd-php',
  74.             'php3'    =>    'application/x-httpd-php',
  75.             'phtml'    =>    'application/x-httpd-php',
  76.             'phps'    =>    'application/x-httpd-php-source',
  77.             'js'    =>    'application/x-javascript',
  78.             'swf'    =>    'application/x-shockwave-flash',
  79.             'sit'    =>    'application/x-stuffit',
  80.             'tar'    =>    'application/x-tar',
  81.             'tgz'    =>    array('application/x-tar', 'application/x-gzip-compressed'),
  82.             'xhtml'    =>    'application/xhtml+xml',
  83.             'xht'    =>    'application/xhtml+xml',
  84.             'zip'    =>  array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),
  85.             'mid'    =>    'audio/midi',
  86.             'midi'    =>    'audio/midi',
  87.             'mpga'    =>    '',
  88.             'mp2'    =>    '',
  89.             'mp3'    =>    array('', 'audio/mpg', '3', 'audio/mp3'),
  90.             'aif'    =>    'audio/x-aiff',
  91.             'aiff'    =>    'audio/x-aiff',
  92.             'aifc'    =>    'audio/x-aiff',
  93.             'ram'    =>    'audio/x-pn-realaudio',
  94.             'rm'    =>    'audio/x-pn-realaudio',
  95.             'rpm'    =>    'audio/x-pn-realaudio-plugin',
  96.             'ra'    =>    'audio/x-realaudio',
  97.             'rv'    =>    'video/vnd.rn-realvideo',
  98.             'wav'    =>    array('audio/x-wav', 'audio/wave', 'audio/wav'),
  99.             'bmp'    =>    array('image/bmp', 'image/x-windows-bmp'),
  100.             'gif'    =>    'image/gif',
  101.             'jpeg'    =>    array('image/jpeg', 'image/pjpeg'),
  102.             'jpg'    =>    array('image/jpeg', 'image/pjpeg'),
  103.             'jpe'    =>    array('image/jpeg', 'image/pjpeg'),
  104.             'png'    =>    array('image/png',  'image/x-png'),
  105.             'tiff'    =>    'image/tiff',
  106.             'tif'    =>    'image/tiff',
  107.             'css'    =>    'text/css',
  108.             'html'    =>    'text/html',
  109.             'htm'    =>    'text/html',
  110.             'shtml'    =>    'text/html',
  111.             'txt'    =>    'text/plain',
  112.             'text'    =>    'text/plain',
  113.             'log'    =>    array('text/plain', 'text/x-log'),
  114.             'rtx'    =>    'text/richtext',
  115.             'rtf'    =>    'text/rtf',
  116.             'xml'    =>    'text/xml',
  117.             'xsl'    =>    'text/xml',
  118.             'mpeg'    =>    'video/mpeg',
  119.             'mpg'    =>    'video/mpeg',
  120.             'mpe'    =>    'video/mpeg',
  121.             'qt'    =>    'video/quicktime',
  122.             'mov'    =>    'video/quicktime',
  123.             'avi'    =>    'video/x-msvideo',
  124.             'movie'    =>    'video/x-sgi-movie',
  125.             'doc'    =>    'application/msword',
  126.             'docx'    =>    array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),
  127.             'xlsx'    =>    array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'),
  128.             'word'    =>    array('application/msword', 'application/octet-stream'),
  129.             'xl'    =>    'application/excel',
  130.             'eml'    =>    'message/rfc822',
  131.             'json' => array('application/json', 'text/json')
  132.         );
复制代码
看到这里估计大家都懂了,就是由后缀去匹配文件类型。实现mime_content_type的功能。

文章来源: blog.csdn.net,作者:lxw1844912514,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/lxw1844912514/article/details/100026721

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。