标签:blog io ar color for 文件 on div 2014
<?php
/**
*
* @name upload.class.php
* 上传文件类
* @author Peter
* @createtime 2014-11-20 10:30:29
*
*/
class upload{
public $max_size; //上传file大小
public $allow_types; //上传文件类型
public $file_name; //文件名
public $errmsg; //错误提示
public $uploaded; //后的文件名
public $save_path; //上传文件保存路径
private $file_type; //文件类型
private $ext; //上传文件扩展名
/*
* 构造函数
* @access public
* @string $save_path 上传目标文件
* @string $file_name 上传后的文件名
* @string $allow_types 允许上传的文件类型
* */
function __construct($save_path = "./upload/",$file_name=‘date‘,$allow_types = ‘‘)
{
$this->file_name = $file_name;//默认用时间命名
$this->save_path = (preg_match(‘/\/$/‘,$save_path))?$save_path:$save_path.‘/‘;
$this->allow_types = $allow_types == ‘‘ ? ‘jpg|gif|png‘ : $allow_types;
$this->max_size = 1024*1024*2;
}
/*
* 上传文件
* @access public
* @param $file 等待上传的文件
* @return Boolean
* */
public function upload_file($files)
{
$info = $_FILES[$files];
$name = $info[‘name‘];
$type = $info[‘type‘];
$tmp_name = $info[‘tmp_name‘];
$error = $info[‘error‘];
$size = $info[‘size‘];
switch ($error) {
case ‘0‘:
$this->errmsg = ‘‘;
break;
case ‘1‘:
$this->errmsg = ‘文件大小超过了php.ini中的限制‘;
break;
case ‘2‘:
$this->errmsg = ‘文件大小超过了浏览器的限制‘;
break;
case ‘3‘:
$this->errmsg = ‘文件部分被上传‘;
break;
case ‘4‘:
$this->errmsg = ‘没有找到要上传的文件‘;
break;
case ‘5‘:
$this->errmsg = ‘文件写入临时文件出错‘;
break;
default:
$this->errmsg = "文件上传错误";
break;
}
if($error == 0 && is_uploaded_file($tmp_name)){
if($this->check_file_type($name) == false){
return false;
}
if($size > $this->max_size){
$this->errmsg = "上传文件<font color=‘red‘>".$name."</font>太大";
return false;
}
}
$this->set_save_path();
$new_name = $this->file_name != ‘date‘ ? $this->file_name.‘.‘.$this->ext : date(‘YmdHis‘).‘.‘.$this->ext;//设置新文件名
$this->uploaded = $this->save_path.$new_name;//上传后的文件名
//移动文件
if(move_uploaded_file($tmp_name,$this->uploaded)){
$this->errmsg = ‘文件<font color=red>‘.$this->uploaded.‘</font>上传成功!‘;
return true;
}else{
$this->errmsg = ‘文件<font color=red>‘.$this->uploaded.‘</font>上传失败!‘;
return false;
}
}
/*
* 检查上传文件类型
* @access private
* @param string $filename 等待检查的文件名
* @return 如果通过返回true 不通过返回false
* */
private function check_file_type($filename)
{
$ext = $this->get_file_type($filename);
$this->ext = $ext;
$allow_types = explode(‘|‘, $this->allow_types);//允许上传文件类型用|分割
if(in_array($ext, $allow_types)){
return true;
}else{
$this->errmsg = "上传文件".$filename."类型错误";
return false;
}
}
/*
* 取得上传文件类型
* @access private
* @param string $filename 要取得文件类型的文件名
* @return string 文件类型
* */
private function get_file_type($filename)
{
$info = pathinfo($filename);
$ext = $info[‘extension‘];
return $ext;
}
/*
*设置上传后的保存路径
* */
private function set_save_path()
{
$this->save_path = (preg_match(‘/\/$/‘,$this->save_path)) ? $this->save_path : $this->save_path . ‘/‘;
if(!is_dir($this->save_path)){
//如果目录不存在,创建目录
$this->set_dir($this->save_path);
}
}
/*
* 创建目录
* @access private
* @param string $dir 要创建目录的路径
* @return boolean 失败的时候返回fales
* */
private function set_dir($dir)
{
//检查路径是否存在
if(is_dir($dir)){
$this->errmsg = ‘需要创建的文件夹已经存在!‘;
}
$dir = explode(‘/‘, $dir);
$d = "";
foreach($dir as $v){
if($v){
$d .= $v . ‘/‘;
if(!is_dir($d)){
$state = mkdir($d, 0777);
if(!$state)
$this->errmsg = ‘在创建目录<font color=red>‘ . $d . ‘时出错!‘;
}
}
}
return true;
}
}
/*End of file upload.class.php*/
标签:blog io ar color for 文件 on div 2014
原文地址:http://www.cnblogs.com/peter767/p/4110166.html