[源码]文件下载类

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
<?    
//====================================================
// 使用范例:
// $download=new download('php,exe,html',false);
// if(!$download->downloadfile($filename))
// {
// echo $download->geterrormsg();
// }
//====================================================
class download{
var $debug=true;
var $errormsg='';
var $Filter=array();
var $filename='';
var $mineType='text/plain';
var $xlq_filetype=array();

function download($fileFilter='',$isdebug=true)
{
$this->setFilter($fileFilter);
$this->setdebug($isdebug);
$this->setfiletype();
}

function setFilter($fileFilter)
{
if(empty($fileFilter)) return ;
$this->Filter=explode(',',strtolower($fileFilter));
}
function setdebug($debug)
{
$this->debug=$debug;
}

function setfilename($filename)
{
$this->filename=$filename;
}

function downloadfile($filename)
{
$this->setfilename($filename);
if($this->filecheck())
{
$fn = array_pop( explode( '/', strtr( $this->filename, '\\', '/' ) ) );
header( "Pragma: public" );
header( "Expires: 0" ); // set expiration time
header( "Cache-Component: must-revalidate, post-check=0, pre-check=0" );
header( "Content-type:".$this->mineType );
header( "Content-Length: " . filesize( $this->filename ) );
header( "Content-Disposition: attachment; filename="$fn"" );
header( 'Content-Transfer-Encoding: binary' );
readfile( $this->filename );
return true;
}else
{
return false;
}
}
function geterrormsg()
{
return $this->errormsg;
}

function filecheck()
{
$filename=$this->filename;
if(file_exists($filename))
{
$filetype=strtolower(array_pop(explode('.',$filename)));
if(in_array($filetype,$this->Filter))
{
$this->errormsg.=$filename.'不允许下载!';
if($this->debug) exit($filename.'不允许下载!') ;
return false;
}else
{
if ( function\_exists( "mime\_content_type" ) )
{
$this->mineType = mime\_content\_type( $filename );
}
if(empty($this->mineType))
{
if( isset($this->xlq\_filetype\[$filetype\]) ) $this->mineType = $this->xlq\_filetype\[$filetype\];
}
if(!empty($this->mineType))
return true;
else
{
$this->errormsg.='获取'.$filename.'文件类型时候发生错误,或者不存在预定文件类型内';
if($this->debug) exit('获取文件类型出错');
return false;
}
}
}else
{
$this->errormsg.=$filename.'不存在!';
if($this->debug) exit($filename.'不存在!') ;
return false;
}
}

function setfiletype()
{
$this->xlq_filetype\['chm'\]='application/octet-stream';
$this->xlq_filetype\['ppt'\]='application/vnd.ms-powerpoint';
$this->xlq_filetype\['xls'\]='application/vnd.ms-excel';
$this->xlq_filetype\['doc'\]='application/msword';
$this->xlq_filetype\['exe'\]='application/octet-stream';
$this->xlq_filetype\['rar'\]='application/octet-stream';
$this->xlq_filetype\['js'\]="javascript/js";
$this->xlq_filetype\['css'\]="text/css";
$this->xlq_filetype\['hqx'\]="application/mac-binhex40";
$this->xlq_filetype\['bin'\]="application/octet-stream";
$this->xlq_filetype\['oda'\]="application/oda";
$this->xlq_filetype\['pdf'\]="application/pdf";
$this->xlq_filetype\['ai'\]="application/postsrcipt";
$this->xlq_filetype\['eps'\]="application/postsrcipt";
$this->xlq_filetype\['es'\]="application/postsrcipt";
$this->xlq_filetype\['rtf'\]="application/rtf";
$this->xlq_filetype\['mif'\]="application/x-mif";
$this->xlq_filetype\['csh'\]="application/x-csh";
$this->xlq_filetype\['dvi'\]="application/x-dvi";
$this->xlq_filetype\['hdf'\]="application/x-hdf";
$this->xlq_filetype\['nc'\]="application/x-netcdf";
$this->xlq_filetype\['cdf'\]="application/x-netcdf";
$this->xlq_filetype\['latex'\]="application/x-latex";
$this->xlq_filetype\['ts'\]="application/x-troll-ts";
$this->xlq_filetype\['src'\]="application/x-wais-source";
$this->xlq_filetype\['zip'\]="application/zip";
$this->xlq_filetype\['bcpio'\]="application/x-bcpio";
$this->xlq_filetype\['cpio'\]="application/x-cpio";
$this->xlq_filetype\['gtar'\]="application/x-gtar";
$this->xlq_filetype\['shar'\]="application/x-shar";
$this->xlq_filetype\['sv4cpio'\]="application/x-sv4cpio";
$this->xlq_filetype\['sv4crc'\]="application/x-sv4crc";
$this->xlq_filetype\['tar'\]="application/x-tar";
$this->xlq_filetype\['ustar'\]="application/x-ustar";
$this->xlq_filetype\['man'\]="application/x-troff-man";
$this->xlq_filetype\['sh'\]="application/x-sh";
$this->xlq_filetype\['tcl'\]="application/x-tcl";
$this->xlq_filetype\['tex'\]="application/x-tex";
$this->xlq_filetype\['texi'\]="application/x-texinfo";
$this->xlq_filetype\['texinfo'\]="application/x-texinfo";
$this->xlq_filetype\['t'\]="application/x-troff";
$this->xlq_filetype\['tr'\]="application/x-troff";
$this->xlq_filetype\['roff'\]="application/x-troff";
$this->xlq_filetype\['shar'\]="application/x-shar";
$this->xlq_filetype\['me'\]="application/x-troll-me";
$this->xlq_filetype\['ts'\]="application/x-troll-ts";
$this->xlq_filetype\['gif'\]="image/gif";
$this->xlq_filetype\['jpeg'\]="image/pjpeg";
$this->xlq_filetype\['jpg'\]="image/pjpeg";
$this->xlq_filetype\['jpe'\]="image/pjpeg";
$this->xlq_filetype\['ras'\]="image/x-cmu-raster";
$this->xlq_filetype\['pbm'\]="image/x-portable-bitmap";
$this->xlq_filetype\['ppm'\]="image/x-portable-pixmap";
$this->xlq_filetype\['xbm'\]="image/x-xbitmap";
$this->xlq_filetype\['xwd'\]="image/x-xwindowdump";
$this->xlq_filetype\['ief'\]="image/ief";
$this->xlq_filetype\['tif'\]="image/tiff";
$this->xlq_filetype\['tiff'\]="image/tiff";
$this->xlq_filetype\['pnm'\]="image/x-portable-anymap";
$this->xlq_filetype\['pgm'\]="image/x-portable-graymap";
$this->xlq_filetype\['rgb'\]="image/x-rgb";
$this->xlq_filetype\['xpm'\]="image/x-xpixmap";
$this->xlq_filetype\['txt'\]="text/plain";
$this->xlq_filetype\['c'\]="text/plain";
$this->xlq_filetype\['cc'\]="text/plain";
$this->xlq_filetype\['h'\]="text/plain";
$this->xlq_filetype\['html'\]="text/html";
$this->xlq_filetype\['htm'\]="text/html";
$this->xlq_filetype\['htl'\]="text/html";
$this->xlq_filetype\['rtx'\]="text/richtext";
$this->xlq_filetype\['etx'\]="text/x-setext";
$this->xlq_filetype\['tsv'\]="text/tab-separated-values";
$this->xlq_filetype\['mpeg'\]="video/mpeg";
$this->xlq_filetype\['mpg'\]="video/mpeg";
$this->xlq_filetype\['mpe'\]="video/mpeg";
$this->xlq_filetype\['avi'\]="video/x-msvideo";
$this->xlq_filetype\['qt'\]="video/quicktime";
$this->xlq_filetype\['mov'\]="video/quicktime";
$this->xlq_filetype\['moov'\]="video/quicktime";
$this->xlq_filetype\['movie'\]="video/x-sgi-movie";
$this->xlq_filetype\['au'\]="audio/basic";
$this->xlq_filetype\['snd'\]="audio/basic";
$this->xlq_filetype\['wav'\]="audio/x-wav";
$this->xlq_filetype\['aif'\]="audio/x-aiff";
$this->xlq_filetype\['aiff'\]="audio/x-aiff";
$this->xlq_filetype\['aifc'\]="audio/x-aiff";
$this->xlq_filetype\['swf'\]="application/x-shockwave-flash";
}
}
?>
0%