日期:2014-05-17 浏览次数:20605 次
<?php
/**
* file download class
* Create Time:06/23/2009
* Author:DAKER.W
* QQ:451021477
*/
class Download{
private $debug = false;
private $errorMsg = '';
private $filter = array();
private $fileName = '';
private $mineType = '';
private $xlq_filetype = array();
private $limitTime = 60;
/**
* @param string $fileFilter
* @param boolean $isDebug
*/
function __construct($fileFilter='', $isDebug=true)
{
$this->setFilter($fileFilter);
$this->setDebug($isDebug);
$this->setFileType();
}
function downloadfile($filename)
{
$this->fileName = $filename;
if($this->filecheck()){
$fn = basename($this->fileName);
ob_end_clean();
@set_time_limit($this->limitTime);
header('Cache-control: max-age=31536000');
header('Expires: '.gmdate('D, d M Y H:i:s',time()+31536000).' GMT');
header('Content-Encoding: none');
header('Content-Length: '.filesize($this->fileName));
header('Content-Disposition: attachment; filename='.$fn);
header('Content-Type: '.$this->mineType);
readfile($this->fileName);
return true;
}else{
return false;
}
}
function filecheck()
{
$fileName = $this->fileName;
if(file_exists($fileName)){
$fileType = strtolower(array_pop(explode(".", $fileName)));
if(!in_array($fileType, $this->filter)){
$this->errorMsg .= sprintf("%s can't download!", $fileName);
if($this->debug)exit(sprintf("%s can't download!", $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 .= "Can't get file type!";
if($this->debug)exit("Can't get file type!");
return false;
}
}
}else {
$this->errorMsg .= sprintf("%s isn't exists!", $fileName);
if($this->debug)exit(sprintf("%s isn't exists!", $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']="javascrīpt/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']=