php下载文件工具类支持中文名称(兼容)

一个兼容中文名称的下载文件工具类

知兮丶青 工具类
阅读(319) 2017-12-11
php下载文件工具类支持中文名称(兼容)
php下载文件工具类支持中文名称(兼容)

摘录个好用的php下载方法,放哪哪用。

HttpUtil类摘录于thinkphp3.2。

因为thinkphp5.0无该方法,所以挪来用了。

觉得真心好用就在此分享、备忘下。


类已经我优化了几个地方使得兼容中文名称,和兼容主流浏览器下载显示的文件中文名称。


下载代码

HttpUtil.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * HTTP工具类
 * @author cxq 微知兮
 */
class HttpUtil{
 
    /**
     * 下载文件
     * 可以指定下载显示的文件名,并自动发送相应的Header信息
     * 如果指定了content参数,则下载该参数的内容
     * @static
     * @access public
     * @param string $filename 下载文件名
     * @param string $showname 下载显示的文件名
     * @param string $content  下载的内容
     * @param integer $expire  下载内容浏览器缓存时间
     * @throws Exception
     * @return void
     */
    static public function download($filename$showname='',$cOntent='',$expire=180) {
        if(is_file($filename)){
            $length filesize($filename);
        }else if($content != ''){
            $length strlen($content);
        }else{
            throw new Exception('文件不存在');
        }
        if(empty($showname)){
            $showname $filename;
        }
        $showname = ltrim(substr($shownamestrrpos($showname'/')),"/");
        if(!empty($filename)){
            $type = mime_content_type($filename);
        }else{
            $type "application/octet-stream";
        }
        $showname = urlencode($showname);
        $showname str_replace("+""%20"$showname);
        //发送Http Header信息 开始下载
        header("Pragma: public");
        header("Cache-control: max-age=".$expire);
        //header('Cache-Control: no-store, no-cache, must-revalidate');
        header("Expires: " gmdate("D, d M Y H:i:s",time()+$expire) . "GMT");
        header("Last-Modified: " gmdate("D, d M Y H:i:s",time()) . "GMT");
        //header("Content-Disposition: attachment; filename=".$showname);
        $ua $_SERVER['HTTP_USER_AGENT'];
        if(preg_match("/MSIE/"$ua)){
            header('Content-Disposition: attachment; filename="' $showname '"');
        }else if(preg_match("/Firefox/"$ua)){
            header('Content-Disposition: attachment; filename*="utf8\'\'' $showname '"');
        }else{
            header('Content-Disposition: attachment; filename="' $showname '"');
        }
        header("Content-Length: ".$length);
        header("Content-type: ".$type);
        header('Content-Encoding: none');
        header("Content-Transfer-Encoding: binary" );
        if($content == '' ){
            readfile($filename);
        }else{
            echo($content);
        }
        exit();
    }
 
    /**
     * 采集远程文件
     * @access public
     * @param string $remote 远程文件名
     * @param string $local 本地保存文件名
     * @return mixed
     */
    static public function curlDownload($remote$local) {
        $cp = curl_init($remote);
        $fp fopen($local,"w");
        curl_setopt($cp, CURLOPT_FILE, $fp);
        curl_setopt($cp, CURLOPT_HEADER, 0);
        curl_exec($cp);
        curl_close($cp);
        fclose($fp);
    }
 
}

如果想更准确的Content-type类型 且 又版本不支持mime_content_type的话,请加入以下代码:

,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//类定义结束
if( !function_exists ('mime_content_type')) {
    /**
     * 获取文件的mime_content类型
     * @return string
     */
    function mime_content_type($filename) {
        static $contentType array(
            'ai'        => 'application/postscript',
            'aif'       => 'audio/x-aiff',
            'aifc'      => 'audio/x-aiff',
            'aiff'      => 'audio/x-aiff',
            'asc'       => 'application/pgp'//changed by skwashd - was text/plain
            'asf'       => 'video/x-ms-asf',
            'asx'       => 'video/x-ms-asf',
            'au'        => 'audio/basic',
            'avi'       => 'video/x-msvideo',
            'bcpio'     => 'application/x-bcpio',
            'bin'       => 'application/octet-stream',
            'bmp'       => 'image/bmp',
            'c'         => 'text/plain'// or 'text/x-csrc', //added by skwashd
            'cc'        => 'text/plain'// or 'text/x-c++src', //added by skwashd
            'cs'        => 'text/plain'//added by skwashd - for C# src
            'cpp'       => 'text/x-c++src'//added by skwashd
            'cxx'       => 'text/x-c++src'//added by skwashd
            'cdf'       => 'application/x-netcdf',
            'class'     => 'application/octet-stream',//secure but application/java-class is correct
            'com'       => 'application/octet-stream',//added by skwashd
            'cpio'      => 'application/x-cpio',
            'cpt'       => 'application/mac-compactpro',
            'csh'       => 'application/x-csh',
            'css'       => 'text/css',
            'csv'       => 'text/comma-separated-values',//added by skwashd
            'dcr'       => 'application/x-director',
            'diff'      => 'text/diff',
            'dir'       => 'application/x-director',
            'dll'       => 'application/octet-stream',
            'dms'       => 'application/octet-stream',
            'doc'       => 'application/msword',
            'dot'       => 'application/msword',//added by skwashd
            'dvi'       => 'application/x-dvi',
            'dxr'       => 'application/x-director',
            'eps'       => 'application/postscript',
            'etx'       => 'text/x-setext',
            'exe'       => 'application/octet-stream',
            'ez'        => 'application/andrew-inset',
            'gif'       => 'image/gif',
            'gtar'      => 'application/x-gtar',
            'gz'        => 'application/x-gzip',
            'h'         => 'text/plain'// or 'text/x-chdr',//added by skwashd
            'h++'       => 'text/plain'// or 'text/x-c++hdr', //added by skwashd
            'hh'        => 'text/plain'// or 'text/x-c++hdr', //added by skwashd
            'hpp'       => 'text/plain'// or 'text/x-c++hdr', //added by skwashd
            'hxx'       => 'text/plain'// or 'text/x-c++hdr', //added by skwashd
            'hdf'       => 'application/x-hdf',
            'hqx'       => 'application/mac-binhex40',
            'htm'       => 'text/html',
            'html'      => 'text/html',
            'ice'       => 'x-conference/x-cooltalk',
            'ics'       => 'text/calendar',
            'ief'       => 'image/ief',
            'ifb'       => 'text/calendar',
            'iges'      => 'model/iges',
            'igs'       => 'model/iges',
            'jar'       => 'application/x-jar'//added by skwashd - alternative mime type
            'java'      => 'text/x-java-source'//added by skwashd
            'jpe'       => 'image/jpeg',
            'jpeg'      => 'image/jpeg',
            'jpg'       => 'image/jpeg',
            'js'        => 'application/x-Javascript',
            'kar'       => 'audio/midi',
            'latex'     => 'application/x-latex',
            'lha'       => 'application/octet-stream',
            'log'       => 'text/plain',
            'lzh'       => 'application/octet-stream',
            'm3u'       => 'audio/x-mpegurl',
            'man'       => 'application/x-troff-man',
            'me'        => 'application/x-troff-me',
            'mesh'      => 'model/mesh',
            'mid'       => 'audio/midi',
            'midi'      => 'audio/midi',
            'mif'       => 'application/vnd.mif',
            'mov'       => 'video/quicktime',
            'movie'     => 'video/x-sgi-movie',
            'mp2'       => 'audio/mpeg',
            'mp3'       => 'audio/mpeg',
            'mpe'       => 'video/mpeg',
            'mpeg'      => 'video/mpeg',
            'mpg'       => 'video/mpeg',
            'mpga'      => 'audio/mpeg',
            'ms'        => 'application/x-troff-ms',
            'msh'       => 'model/mesh',
            'mxu'       => 'video/vnd.mpegurl',
            'nc'        => 'application/x-netcdf',
            'oda'       => 'application/oda',
            'patch'     => 'text/diff',
            'pbm'       => 'image/x-portable-bitmap',
            'pdb'       => 'chemical/x-pdb',
            'pdf'       => 'application/pdf',
            'pgm'       => 'image/x-portable-graymap',
            'pgn'       => 'application/x-chess-pgn',
            'pgp'       => 'application/pgp',//added by skwashd
            'php'       => 'application/x-httpd-php',
            'php3'      => 'application/x-httpd-php3',
            'pl'        => 'application/x-perl',
            'pm'        => 'application/x-perl',
            'png'       => 'image/png',
            'pnm'       => 'image/x-portable-anymap',
            'po'        => 'text/plain',
            'ppm'       => 'image/x-portable-pixmap',
            'ppt'       => 'application/vnd.ms-powerpoint',
            'ps'        => 'application/postscript',
            'qt'        => 'video/quicktime',
            'ra'        => 'audio/x-realaudio',
            'rar'       => 'application/octet-stream',
            'ram'       => 'audio/x-pn-realaudio',
            'ras'       => 'image/x-cmu-raster',
            'rgb'       => 'image/x-rgb',
            'rm'        => 'audio/x-pn-realaudio',
            'roff'      => 'application/x-troff',
            'rpm'       => 'audio/x-pn-realaudio-plugin',
            'rtf'       => 'text/rtf',
            'rtx'       => 'text/richtext',
            'sgm'       => 'text/sgml',
            'sgml'      => 'text/sgml',
            'sh'        => 'application/x-sh',
            'shar'      => 'application/x-shar',
            'shtml'     => 'text/html',
            'silo'      => 'model/mesh',
            'sit'       => 'application/x-stuffit',
            'skd'       => 'application/x-koan',
            'skm'       => 'application/x-koan',
            'skp'       => 'application/x-koan',
            'skt'       => 'application/x-koan',
            'smi'       => 'application/smil',
            'smil'      => 'application/smil',
            'snd'       => 'audio/basic',
            'so'        => 'application/octet-stream',
            'spl'       => 'application/x-futuresplash',
            'src'       => 'application/x-wais-source',
            'stc'       => 'application/vnd.sun.xml.calc.template',
            'std'       => 'application/vnd.sun.xml.draw.template',
            'sti'       => 'application/vnd.sun.xml.impress.template',
            'stw'       => 'application/vnd.sun.xml.writer.template',
            'sv4cpio'   => 'application/x-sv4cpio',
            'sv4crc'    => 'application/x-sv4crc',
            'swf'       => 'application/x-shockwave-flash',
            'sxc'       => 'application/vnd.sun.xml.calc',
            'sxd'       => 'application/vnd.sun.xml.draw',
            'sxg'       => 'application/vnd.sun.xml.writer.global',
            'sxi'       => 'application/vnd.sun.xml.impress',
            'sxm'       => 'application/vnd.sun.xml.math',
            'sxw'       => 'application/vnd.sun.xml.writer',
            't'         => 'application/x-troff',
            'tar'       => 'application/x-tar',
            'tcl'       => 'application/x-tcl',
            'tex'       => 'application/x-tex',
            'texi'      => 'application/x-texinfo',
            'texinfo'   => 'application/x-texinfo',
            'tgz'       => 'application/x-gtar',
            'tif'       => 'image/tiff',
            'tiff'      => 'image/tiff',
            'tr'        => 'application/x-troff',
            'tsv'       => 'text/tab-separated-values',
            'txt'       => 'text/plain',
            'ustar'     => 'application/x-ustar',
            'vbs'       => 'text/plain'//added by skwashd - for obvious reasons
            'vcd'       => 'application/x-cdlink',
            'vcf'       => 'text/x-vcard',
            'vcs'       => 'text/calendar',
            'vfb'       => 'text/calendar',
            'vrml'      => 'model/vrml',
            'vsd'       => 'application/vnd.visio',
            'wav'       => 'audio/x-wav',
            'wax'       => 'audio/x-ms-wax',
            'wbmp'      => 'image/vnd.wap.wbmp',
            'wbxml'     => 'application/vnd.wap.wbxml',
            'wm'        => 'video/x-ms-wm',
            'wma'       => 'audio/x-ms-wma',
            'wmd'       => 'application/x-ms-wmd',
            'wml'       => 'text/vnd.wap.wml',
            'wmlc'      => 'application/vnd.wap.wmlc',
            'wmls'      => 'text/vnd.wap.wmlscript',
            'wmlsc'     => 'application/vnd.wap.wmlscriptc',
            'wmv'       => 'video/x-ms-wmv',
            'wmx'       => 'video/x-ms-wmx',
            'wmz'       => 'application/x-ms-wmz',
            'wrl'       => 'model/vrml',
            'wvx'       => 'video/x-ms-wvx',
            'xbm'       => 'image/x-xbitmap',
            'xht'       => 'application/xhtml+xml',
            'xhtml'     => 'application/xhtml+xml',
            'xls'       => 'application/vnd.ms-excel',
            'xlt'       => 'application/vnd.ms-excel',
            'xml'       => 'application/xml',
            'xpm'       => 'image/x-xpixmap',
            'xsl'       => 'text/xml',
            'xwd'       => 'image/x-xwindowdump',
            'xyz'       => 'chemical/x-xyz',
            'z'         => 'application/x-compress',
            'zip'       => 'application/zip',
        );
        $type strtolower(substr(strrchr($filename'.'),1));
        if(isset($contentType[$type])) {
            $mime $contentType[$type];
        }else {
            $mime 'application/octet-stream';
        }
        return $mime;
    }
}


,

使用方法

下载文件

1
HttpUtil::download("C:/Users/XQ/Desktop/1.jpg""中文名字.jpg");

直接使用字符串作为文件下载

1
HttpUtil::download("""1.txt""写入文件里面的文字ABC123");




已下载:262

原创文章,转载请注明出处:https://www.weizhixi.com/article/29.html