标签:
/*
*@文件上传
*@要获取上传实例 请使用 Upload::getInstance(); 具体使用方法请参考类接口
*@auther 张文庆
*@email 812035863@qq.com
*@blog www.92zyw.com
* */
class Upload{
private static $instance = null;
protected $destDir; //上传文件的存放目录
protected $allowType; //允许上传的类型
protected $denyType; //拒绝上传文件的类型
protected $mutilUpload; //是否允许多文件上传
protected $maxUpload; //最大上传字节数
private function __construct(array $options=null){
$this->destDir = ‘upload‘;/* str_replace(‘\\‘,‘/‘,strstr(getcwd(),‘test‘,true)); */
$this->allowType = array(‘jpg‘,‘gif‘);
$this->denyType = array(‘php‘);
$this->mutilUpload=false;
$this->maxUpload = ‘202800‘;
is_dir($this->destDir) or mkdir($this->destDir);
if(isset($options)){
foreach($options as $k =>$v){
$this->$k = $v;
}
}
}
//单例模式
public static function getInstance(array $options=null){
if(static::$instance == null){
if(isset($options)){
static::$instance = new Upload($options);
}else{
static::$instance = new Upload();
}
}
return static::$instance;
}
/* *
* 上传单个文件
* @param array
*
* */
public function upload(array $file){
try {
return $this->move($file[‘name‘],$file[‘tmp_name‘]);
} catch (UploadException $e) {
ExceptionPage::showMsg($e.getMessage(),$e->getErrno);
}
}
/*
* @多文件上传
* @接收的参数是数组 格式 如: array(array(‘name‘=>$file[‘name],‘tmp_name‘=>$file[‘tmp_file‘]))
*
* */
public function mutilUpload(array $files){
if(!$this->uploadAvailable()){
throw new UploadException("不允许进行多文件上传");
return false;
}
foreach($files as $file){
if(!isset($file) && !is_array($file)){
throw new UploadException(‘请确认数组参数是否符合多文件上传的要求!‘);
}
$this->move($file[‘name‘],$file[‘tmp_name‘]);
}
return true;
}
/* @复制文件
* @param string
* @param string
* */
protected function move($file,$tmp_name){
$filename = $this->destDir.$file;
$fileTmpName = date(‘Ymdhis‘,time()).‘.‘.pathinfo($file)[‘extension‘];
$result = move_uploaded_file($tmp_name,"{$this->destDir}/{$fileTmpName}");
if(!result){
throw new UploadException(‘文件不存在或者是由于一些不可预知的原因而无法移动文件‘);
}
return $result;
}
protected function uploadAvailable($file){
$size = filesize($file);
if($size >$this->maxUpload){
return false;
}
return false;
}
}
/* 异常类 */
class Expection{
protected $msg;
protected $errno;
public function getMessage(){
return $this->msg;
}
public function getErrno(){
return $this->errno;
}
}
/* 上传异常类 */
class UploadExpection extends Expection{
public function __construct($msg,$errno=null){
$this->msg = $msg;
if(isset($errno)){
$this->errno =null;
}else{
$this->errno = $errno;
}
}
}
class ExceptionPage{
static $instance = null;
public $html=‘‘;
public function __construct($msg,$errno=‘‘){
$html .=‘<html en="en-Ch">‘;
$html .=‘<head>‘;
$html .=‘<title>异常页面</title>‘;
$html .=‘</head>‘;
$html .=‘<body>‘;
$html .="<div><p>异常:{$msg}</p>";
if(isset($errno)){
$html .=‘<p>异常代码:{$errno}</p>‘;
}
$html .=‘</div>‘;
$html .=‘</body>‘;
$html .=‘</html>‘;
echo $html;
}
public static function showMsg($msg,$errno){
new Exception($msg,$errno);
}
}
标签:
原文地址:http://www.cnblogs.com/Super-Man/p/4218865.html