学习文件下载,我们下载的时候mime类型非常不好判断 所以我写的类就直接加进去了,扩展性非常强,如果您有什么建议或者需要什么功能我会以后添加,当然初步测试用,
<?php
/*
*文件下载类 powered by widuu
*测试用 wdphp框架开发 作者肖伟
*/
class download{
private $filename;
function __construct($filename){
$this->filename=$filename;
}
function download(){
header("Content-Type:".$this->mime($this->filename));
header('Content-Disposition: attachment; filename='.$this->filename);
header('Content-Length:'.filesize($this->filename));
readfile($this->filename);
}
private function mime($filename){
$mime=array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
);
$file=explode('.',$this->filename);
if($dir=in_array($file[1],array_keys($mime))){
return $mime[$file[1]];
}else{
die("不是允许下载的类型");
}
}
}
/*测试用
$download=new download("cache.txt");
$download->download();
*/
?>
欢迎转载,转载请注明来自微度网络http://yun.widuu.com

发表评论 取消回复