Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1649559
  • 博文数量: 1481
  • 博客积分: 26784
  • 博客等级: 上将
  • 技术积分: 17045
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-12 09:22
文章分类

全部博文(1481)

文章存档

2014年(10)

2013年(353)

2012年(700)

2011年(418)

分类: 系统运维

2012-06-04 16:17:49

类文件:Upload.class.php


/* *文件上传类
* *处理单文件及多文件上传
* *上传表单为$_FIFLES[]
* *author:徐浩
* *date:2012-05-28
*
*/
class Upload{
//定义相关属性值
protected $files; //超全局数组$_FIFLES
protected $arrNum; //是否为多元数组,
protected $list = array(); //过滤控制列表,记录有效的上传文件下标
protected $filename; //文件名
protected $size; //大小
protected $type; //类型
protected $newName; //成功上传后的新名称
protected $upfile; //保存路径
protected $num; //预上传文件个数
protected $wNum; //成功上传文件个数
protected $typeList = array(); //文件上传类型控制
protected $res = array(); //上传信息存放数组


//构造函数,实例化自动初始化
//@param array $files
public function __construct($files,$upfile,$typeList){
$this->files = $files;
$this->typeList = $typeList;
//判断上传文件个数,即$files是多维数组
$this->arrNum = count($this->files["name"]);
if($this->arrNum ==="0"){
die("错误:没有文件!");
}
if($this->arrNum>1){ //如果为多维数组,则以数组形式给定义的属性赋值
$this->num = 0;
$list = array();
for($i=0;$i<$this->arrNum;$i++){
if(($this->files["error"][$i])==4)
continue;
$this->filename[] = $this->files["name"][$i];
$this->type[] = $this->files["type"][$i];
//$this->error[] = $this->files["error"][$i];
$this->num++;
$this->list[]=$i;
}
}else{ //如果为一维数组,直接给属性赋值
$this->filename = $this->files["name"];
$this->type = $this->files["type"];
//$this->error = $this->files["error"];
}
$this->upfile = $upfile;
$this->checkUpFile();

}




/*判断上传目录是否存在,如果不存在,尝试创建
*/
public function checkUpFile(){
if(!is_dir($this->upfile)){
if(mkdir($this->upfile)){
$this->res["upfile"] = "上传目录不存在,执行创建【{$this->upfile}】目录成功";
return $this->res;
}else{
$this->res["upfile"] = "执行创建【{$this->upfile}】目录失败";
return $this->res;
die("上传操作终止!");
}
}
}


/*上传类型判定
*如果满足$this->typeList条件,返回false
*如果不满足$this->typeList条件,返回true
*/

public function checkType($type){
if(!in_array($type,$this->typeList)){
return false;
}else{
return true;
}
}


/*上传错误判定
*$error 上传文件$_FILES['error']
*值为0时表示文件上传成功,返回true
*值为非0时表示文件上传失败,通过对应的值返回错误类型信息
*/

public function checkErr($error){
if($error!=0){
switch($error){
case 1:
$info="上传的文件过大,请返回重新添加其他文件";
break;
case 2:
$info="上传的文件过大,请返回重新添加其他文件";
break;
case 3:
$info="上传失败:文件只有部分被上传";
break;
case 4:
$info="错误:没有文件被上传";
break;
case 6:
$info="错误:找不到临时文件夹";
break;
case 7:
$info="上传失败:文件写入失败";
break;
}
$this->res["info"]=$info;//将错误信息放到返回数组里
return $this->res;
}else{
return true;
}
}


/*文件大小自动转换
*$return @int 数值
*$suffix @string 单位
*/
public function getFileSize($bytes){
if($bytes>=pow(2,40)){
$return = round($bytes/pow(1024,4),2);
$suffix = "TB";
}elseif($bytes>=pow(2,30)){
$return = round($bytes/pow(1024,3),2);
$suffix = "GB";
}elseif($bytes>=pow(2,20)){
$return = round($bytes/pow(1024,2),2);
$suffix = "MB";
}elseif($bytes>=pow(2,10)){
$return = round($bytes/pow(1024,1),2);
$suffix = "KB";
}else{
$return = $bytes;
$suffix = "Byte";
}
return $return.$suffix;
}


/*执行文件上传
*$path 解析文件目录结构
*$exten 文件后缀名
*$newFile 拼装新文件名
*$newName 上传后文件全路径
*/
public function add(){
var_dump($this->list);
if($this->num>1){ //如果为多维数组,则以数组形式给定义的属性赋值
for($m=0;$mlist);$m++){
//获得文件后缀名
$path = pathinfo($this->files["name"][$this->list[$m]]);
$exten = $path["extension"];
if($this->checkType($exten) && $this->checkErr($this->files["error"][$this->list[$m]])){
//对上传对象进行存储
do{
$newFile = date("YmdHis").rand(10000,99999).'.'.$exten;
$newName = trim($this->upfile,"/")."/".$newFile;
}while(file_exists($newName));
if(is_uploaded_file($this->files['tmp_name'][$this->list[$m]])){
if(move_uploaded_file($this->files['tmp_name'][$this->list[$m]],$newName)){
$this->res["move"][] = "上传文件成功";
$this->newName[] = $newFile;
$this->size[] = $this->getFileSize($this->files["size"][$this->list[$m]]);
}else{
$this->res["move"][] = "错误:上传文件失败!";
}
}else{
$this->res["move"][] = "错误:上传文件不是一个合法文件:{$this->filename[$this->list[$m]]}";
}
}
}
}else{
$path = pathinfo($this->filename);
$exten = $path["extension"];
if($this->checkType($exten) && $this->checkErr($this->files["error"])){
//对上传对象进行存储
do{
$newFile = date("YmdHis").rand(10000,99999).'.'.$exten;
$newName = trim($this->upfile,"/")."/".$newFile;
}while(file_exists($newName));
if(is_uploaded_file($this->files['tmp_name'])){
if(move_uploaded_file($this->files['tmp_name'],$newName)){
$this->res["move"] = "上传文件成功";
$this->newName = $newFile;
$this->size = $this->getFileSize($this->files["size"]);
}else{
$this->res["move"] = "错误:上传失败!";
}
}else{
$this->res["move"] = "错误:上传文件不是一个合法文件{$this->filename}";
}
}
}
}

//获取上传信息
public function getInfo(){
$str = "";//定义一个字符串变量
var_dump($this->res);
if($this->arrNum>1){
$str = "上传文件新名称:".implode(" | ",$this->newName)."
";

$str .= "上传文件大小:".implode(" | ",$this->size)."
";

$str .= "上传文件个数:".count($this->newName)."
";

$str .= "文件所在文件夹:".$this->upfile."
";

}else{
$str = "上传文件新名称:".$this->newName."
";

$str .= "上传文件大小:".$this->size."
";

$str .= "上传文件个数:".count($this->newName)."
";

$str .= "文件所在文件夹:".$this->upfile."
";

}
echo $str;
}

public function __call($name,$param){
echo "调用的方法【{$name}】不存在";
}


//获得上传信息
public function __get($param){
return $this->$param;
}
}
----------------------
测试:demo.php



文件上传


单个文件上传









多文件上传
















require("Upload.class.php");
$upfile = "./upload/";
$typeList = array("jpeg","jpg","gif","png");
$m = new Upload($_FILES["upload"],$upfile,$typeList);
$m->add();
$m->getInfo();
echo "
";

?>
========================
还没有连接数据库,再测试下。大家如果发现什么纰漏能够指出,多多指教

原文地址:

阅读(203) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~