PHP高级 文件上传类 第36天

2012-11-16 14:21:30 0  category: 第二阶段php

form.html

<form action="up.php" method="post" enctype="multipart/form-data">
<input type="file" name="f[]">
<input type="file" name="f[]">
<input type="file" name="f[]">
<input type="file" name="f[]">
<input type="file" name="f[]">
<input type="file" name="f[]">
<input type="submit" value="提交">
</form>

FileUp.class.php


<?php


//到外面来获得错误

class FileUp{
//文件路径
private $filepath='./';
//文件准许的mime类型
private $allowmime=array('image/jpeg','image/png','image/pjpeg','image/gif','image/jpg','image/x-png','image/wbmp');
//文件准许的后缀
private $allowsubfix=array('jpg','jpeg','gif','bmp','png');
//文件准许的大小
private $maxsize=2000000;
//当前文件名
private $orgname;
//当前文件后缀
private $subfix;
//是否启用随机文件名
private $randname=true;
//文件大小
private $size;
//临时文件名
private $tmpname;
//新文件名
private $newfilename;
//文件类型
private $type;
//错误号
private $errorNum;
//错误信息
private $errorInfo;
//当前文件的mime
private $mime;
//前缀
private $prefix='up_';

function __construct($array=array()){
foreach($array as $key=>$val){
$key=strtolower($key);

if(!array_key_exists($key,get_class_vars(get_class($this))))
continue;
$this->setOption($key,$val);

}

}

//这是一个多文件和单文件自适应上传函数
function upload($field){

//1,检测路径
if(!$this->checkPath()){
return false;
}


//2,我在多文件上传时不能用return false或者return true 停止
$flag=true;

//3,所有属性赋值给相关变量
$name=$_FILES[$field]['name'];
$tmpname=$_FILES[$field]['tmp_name'];
$size=$_FILES[$field]['size'];
$type=$_FILES[$field]['type'];
$error=$_FILES[$field]['error'];


//4,判断这些属性当中的任意一个是否是数组如果是数组,执行多文件上传,如果不是数组执行单文件上传

if(is_array($name)){
for($i=0,$j=count($name);$i<$j;$i++){
if($this->setFiles($name[$i],$size[$i],$type[$i],$error[$i],$tmpname[$i])){
if(!$this->checkSub()|!$this->checkSize()|!$this->checkMime()){
$error[]=$this->getError($this->errorNum);
$flag=false;
}
}else{
$error[]=$this->getError($this->errorNum);
$flag=false;
}
}

for($i=0,$j=count($name);$i<$j;$i++){
if($this->setFiles($name[$i],$size[$i],$type[$i],$error[$i],$tmpname[$i])){
if($this->checkSub()&&$this->checkSize()&&$this->checkMime()){
$this->randname();
$newfilename[]=$this->newfilename;
if(!$this->move()){
$error[]=$this->getError($this->errorNum);
$flag=false;
}

}
}
}

if(!$flag){
$this->errorNum=$error;
}
$this->newfilename=$newfilename;
return $this->newfilename;





}else{
//5,判断是否有上传的文件信息,如果为空有错啊
if(empty($name)){
$this->setOption('errorNum',-3);
}
//6,把刚刚的获得到的信息,赋值给成员属性
if($this->setFiles($name,$size,$type,$error,$tmpname)){
//7,检测大小,mime,后缀
if($this->checkSub()&&$this->checkSize()&&$this->checkMime()){
//8,是否启用随机文件名
$this->randname();
if($this->move()){
return true;
}else{
return false;
}
}else{
return false;
}
}else{
return false;
}
}
}
//9,移动
private function move(){

if(is_uploaded_file($this->tmpname)){
if(move_uploaded_file($this->tmpname,$this->newfilename)){
return true;
}else{
$this->setOption('errorNum',-8);
return false;
}

}else{

$this->setOption('errorNum',-7);
return false;
}

}

private function randname(){
if($this->randname){
$this->newfilename=$this->filepath.$this->createName();
}else{
$this->newfilename=$this->filepath.$this->prefix.$this->orgname;
}

}

private function createName(){

return $this->prefix.uniqid().'.'.$this->subfix;
}

private function checkSub(){
if(in_array($this->subfix,$this->allowsubfix)){

return true;
}else{
$this->setOption('errorNum',-5);
return false;
}

}

private function checkSize(){
if($this->size>$this->maxsize){

$this->setOption('errorNum',-4);
return false;
}else{

return true;
}
}

private function checkMime(){
if(in_array($this->type,$this->allowmime)){

return true;

}else{

$this->setOption('errorNum',-6);
return false;
}

}

private function setFiles($name,$size,$type,$error,$tmpname){
if(!empty($error)){
$this->setOption('errorNum',$error);
return false;
}
$this->orgname=$name;
$this->size=$size;
$this->type=$type;
$this->tmpname=$tmpname;
$arr=explode('.',$name);
$this->subfix=array_pop($arr);
//$this->subfix=$arr[count($arr)-1];

return true;

}

private function checkPath(){
if(empty($this->filepath)){
$this->setOption('errorNum',-2);
return false;
}

$this->filepath=rtrim($this->filepath,'/').'/';
if(!file_exists($this->filepath)|!is_writeable($this->filepath)){
if(mkdir($this->filepath,0777,true)){
return true;
}else{
$this->setOption('errorNum',-1);
return false;
}

}else{

return true;
}

}

private function setOption($key,$val){
$this->$key=$val;
}

public function __get($name){
if($name=='errorInfo'){
return $this->getError($this->errorNum);
}elseif($name=='errorNum'){
return $this->errorNum;
}
}



public function getError($num){
switch($num){
case -1:
$string='文件路径创建失败';
break;
case -2:
$string='文件路径不存在';
break;
case -3:
$string='没有上传文件';
break;
case -4:
$string='超过了手动指定的文件大小';
break;
case -5:
$string='后缀名不准许';
break;
case -6:
$string='文件mime类型不准许';
break;
case -7:
$string='不是上传文件';
break;
case -8:
$string='移动文件失败';
break;
case 1:
$string='文件大小超过了php.ini当中的upload_max_size设定的大小';
break;
case 2:
$string='超过了表单中隐藏域MAX_FILE_SIZE设定的最大值';
break;
case 3:
$string='只有被份文件被上传';
break;
case 4:
$string='没有文件被上传';

break;
case 6:
$string='找不到临时文件夹';
break;
case 7:
$string='文件写入失败';
break;
}
}
}
?>


up.php

<?php
include 'FileUp.class.php';
$up=new FileUp();
$up->upload('f');
$s=$up->errorNum;
var_dump($s);

?>