Home > php教程 > php手册 > body text

Download file summary

WBOY
Release: 2016-08-25 10:19:46
Original
1356 people have browsed it

Generate HTTP headers to force the download of data to the client, which is useful when implementing file downloads.
First of all, thanks to @yangweijie's work, based on which a method was derived that does not require opening the php_fileinfo.dll extension.
Method 1: When the extension is enabled//$file file path<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('Source file does not exist!');<br>          }<br> }Method 2, if php_fileinfo.dll is not turned on, who makes us a programmer without server permissions, and we can’t be looked down upon by others, alas! Maybe you will understand. 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>);Everyone probably understands this, that is, the suffix is ​​used to match the file type. Implement the functions of mime_content_type.
【Advertising】
I undertake the development and secondary development of shopping malls, offices, corporate websites, distribution and other systems. Welcome to consult.
771831851

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template