刚学完对象基础,分享一个上传类[多文件/单文件]

来源:互联网 发布:船长淘宝秒杀辅助器 编辑:程序博客网 时间:2024/06/05 10:33

类文件:Upload.class.php

<?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;$m<count($this->list);$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)."</br/>";
$str .= "上传文件大小:".implode(" | ",$this->size)."<br/>";
$str .= "上传文件个数:".count($this->newName)."个<br/>";
$str .= "文件所在文件夹:".$this->upfile."<br/>";
}else{
$str = "上传文件新名称:".$this->newName."</br/>";
$str .= "上传文件大小:".$this->size."<br/>";
$str .= "上传文件个数:".count($this->newName)."个<br/>";
$str .= "文件所在文件夹:".$this->upfile."<br/>";
}
echo $str;
}

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


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

<html>
<head>
<title>文件上传</title>
</head>
<body>
<h1>单个文件上传</h1>
<form action="demo.php" method="post" enctype="multipart/form-data">
<input type="file" name="upload"/><br/>
<input type="submit" value="上传"/>
</form>
<hr width="100%"/>
<h1>多文件上传</h1>
<form action="demo.php" method="post" enctype="multipart/form-data">
<input type="file" name="upload[]"/><br/>
<input type="file" name="upload[]"/><br/>
<input type="file" name="upload[]"/><br/>
<input type="file" name="upload[]"/><br/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
<?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 "<br/>";
?>
========================
还没有连接数据库,再测试下。大家如果发现什么纰漏能够指出,多多指教

原文地址:http://bbs.lampbrother.net/read-htm-tid-119123.html

阅读(54) | 评论(0) | 转发(0) |
0

上一篇:吐槽下第一项目的总结

下一篇:43,在兄弟连养成的习惯…

相关热门文章
  • IP Sec VPN与NAT破镜重圆
  • 网站导航
  • GoAgent图文设置教程
  • UT2.0正式版下载
  • tomcat6.0配置(含配置视频下载...
  • 大家都是用什么来管理hadoop集...
  • 网站被人挂了吗,添加了些程序...
  • Nginx如何保证不走宕机的那个...
  • 大家谈谈MYSQL客户端和服务器...
  • 以下代码运行后为何会输出5?...
给主人留下些什么吧!~~
原创粉丝点击