Heim > php教程 > php手册 > Hauptteil

下载文件总结

WBOY
Freigeben: 2016-08-25 10:19:46
Original
1301 Leute haben es durchsucht

生成 HTTP 头强制下载数据到客户端,这在实现文件下载时很有用。
首先感谢@yangweijie的作品,在此基础上衍生出不需要开启php_fileinfo.dll扩展的方法。
方法一:开启扩展情况下//$file 文件路径<br> public function download_file($file){<br>         if(is_file($file)){<br>             $length = filesize($file);<br>             $type = mime_content_type($file);<br>             $showname =  ltrim(strrchr($file,'/'),'/');<br>             header("Content-Description: File Transfer");<br>             header('Content-type: ' . $type);<br>             header('Content-Length:' . $length);<br>             if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE<br>                 header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');<br>             } else {<br>                 header('Content-Disposition: attachment; filename="' . $showname . '"');<br>             }<br>             readfile($file);<br>         } else {<br>             $this->error('源文件不存在!');<br>         }<br>     }方法二,如果没有开启php_fileinfo.dll,谁让咱是没有服务器权限的程序员呢,又不能让人家看不起,唉!可能你会懂。public function force_download($filename)<br>     {<br>         if ($filename == ''){<br>             return FALSE;<br>         }<br>         if (FALSE === strpos($filename, '.')){<br>             return FALSE;<br>         }<br>         $x = explode('.', $filename);<br>         $extension = end($x);<br>         $mimes =$this->getMimes();<br> <br>         // Set a default mime if we can't find it<br>         if ( ! isset($mimes[$extension])){<br>             $mime = 'application/octet-stream';<br>         }else{<br>             $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];<br>         }<br>         // Generate the server headers<br>         if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)<br>         {<br>             header('Content-Type: "'.$mime.'"');<br>             header('Content-Disposition: attachment; filename="'.$filename.'"');<br>             header('Expires: 0');<br>             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');<br>             header("Content-Transfer-Encoding: binary");<br>             header('Pragma: public');<br>             header("Content-Length: ".filesize($filename));<br>         }<br>         else<br>         {<br>             header('Content-Type: "'.$mime.'"');<br>             header('Content-Disposition: attachment; filename="'.$filename.'"');<br>             header("Content-Transfer-Encoding: binary");<br>             header('Expires: 0');<br>             header('Pragma: no-cache');<br>             header("Content-Length: ".filesize($filename));<br>         }<br>         readfile($filename);<br>     }<br> private function getMimes(){<br>         return $mimes = array(    'hqx'    =>    'application/mac-binhex40',<br>             'cpt'    =>    'application/mac-compactpro',<br>             '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'),<br>             'bin'    =>    'application/macbinary',<br>             'dms'    =>    'application/octet-stream',<br>             'lha'    =>    'application/octet-stream',<br>             'lzh'    =>    'application/octet-stream',<br>             'exe'    =>    array('application/octet-stream', 'application/x-msdownload'),<br>             'class'    =>    'application/octet-stream',<br>             'psd'    =>    'application/x-photoshop',<br>             'so'    =>    'application/octet-stream',<br>             'sea'    =>    'application/octet-stream',<br>             'dll'    =>    'application/octet-stream',<br>             'oda'    =>    'application/oda',<br>             'pdf'    =>    array('application/pdf', 'application/x-download'),<br>             'ai'    =>    'application/postscript',<br>             'eps'    =>    'application/postscript',<br>             'ps'    =>    'application/postscript',<br>             'smi'    =>    'application/smil',<br>             'smil'    =>    'application/smil',<br>             'mif'    =>    'application/vnd.mif',<br>             'xls'    =>    array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),<br>             'ppt'    =>    array('application/powerpoint', 'application/vnd.ms-powerpoint'),<br>             'wbxml'    =>    'application/wbxml',<br>             'wmlc'    =>    'application/wmlc',<br>             'dcr'    =>    'application/x-director',<br>             'dir'    =>    'application/x-director',<br>             'dxr'    =>    'application/x-director',<br>             'dvi'    =>    'application/x-dvi',<br>             'gtar'    =>    'application/x-gtar',<br>             'gz'    =>    'application/x-gzip',<br>             'php'    =>    'application/x-httpd-php',<br>             'php4'    =>    'application/x-httpd-php',<br>             'php3'    =>    'application/x-httpd-php',<br>             'phtml'    =>    'application/x-httpd-php',<br>             'phps'    =>    'application/x-httpd-php-source',<br>             'js'    =>    'application/x-javascript',<br>             'swf'    =>    'application/x-shockwave-flash',<br>             'sit'    =>    'application/x-stuffit',<br>             'tar'    =>    'application/x-tar',<br>             'tgz'    =>    array('application/x-tar', 'application/x-gzip-compressed'),<br>             'xhtml'    =>    'application/xhtml+xml',<br>             'xht'    =>    'application/xhtml+xml',<br>             'zip'    =>  array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),<br>             'mid'    =>    'audio/midi',<br>             'midi'    =>    'audio/midi',<br>             'mpga'    =>    'audio/mpeg',<br>             'mp2'    =>    'audio/mpeg',<br>             'mp3'    =>    array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'),<br>             'aif'    =>    'audio/x-aiff',<br>             'aiff'    =>    'audio/x-aiff',<br>             'aifc'    =>    'audio/x-aiff',<br>             'ram'    =>    'audio/x-pn-realaudio',<br>             'rm'    =>    'audio/x-pn-realaudio',<br>             'rpm'    =>    'audio/x-pn-realaudio-plugin',<br>             'ra'    =>    'audio/x-realaudio',<br>             'rv'    =>    'video/vnd.rn-realvideo',<br>             'wav'    =>    array('audio/x-wav', 'audio/wave', 'audio/wav'),<br>             'bmp'    =>    array('image/bmp', 'image/x-windows-bmp'),<br>             'gif'    =>    'image/gif',<br>             'jpeg'    =>    array('image/jpeg', 'image/pjpeg'),<br>             'jpg'    =>    array('image/jpeg', 'image/pjpeg'),<br>             'jpe'    =>    array('image/jpeg', 'image/pjpeg'),<br>             'png'    =>    array('image/png',  'image/x-png'),<br>             'tiff'    =>    'image/tiff',<br>             'tif'    =>    'image/tiff',<br>             'css'    =>    'text/css',<br>             'html'    =>    'text/html',<br>             'htm'    =>    'text/html',<br>             'shtml'    =>    'text/html',<br>             'txt'    =>    'text/plain',<br>             'text'    =>    'text/plain',<br>             'log'    =>    array('text/plain', 'text/x-log'),<br>             'rtx'    =>    'text/richtext',<br>             'rtf'    =>    'text/rtf',<br>             'xml'    =>    'text/xml',<br>             'xsl'    =>    'text/xml',<br>             'mpeg'    =>    'video/mpeg',<br>             'mpg'    =>    'video/mpeg',<br>             'mpe'    =>    'video/mpeg',<br>             'qt'    =>    'video/quicktime',<br>             'mov'    =>    'video/quicktime',<br>             'avi'    =>    'video/x-msvideo',<br>             'movie'    =>    'video/x-sgi-movie',<br>             'doc'    =>    'application/msword',<br>             'docx'    =>    array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),<br>             'xlsx'    =>    array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'),<br>             'word'    =>    array('application/msword', 'application/octet-stream'),<br>             'xl'    =>    'application/excel',<br>             'eml'    =>    'message/rfc822',<br>             'json' => array('application/json', 'text/json')<br>         );看到这里估计大家都懂了,就是由后缀去匹配文件类型。实现mime_content_type的功能。
【广告】
本人承接商城,办公,企业站,分销等系统开发及二次开发。欢迎咨询。
771831851

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!