PHP多文件上传类,收藏版!-php文件

PHP多文件上传类,收藏版!

文件名:test.php

<?php

$Str='<form action="uploadfile.php?Send=More" method="post" enctype="multipart/form-data">';

$Str.='文件上传1:<input type="file" name="PicPath[]" /><br>';

$Str.='文件上传2:<input type="file" name="PicPath[]" /><br>';

$Str.='文件上传3:<input type="file" name="PicPath[]" /><br>';

$Str.='文件上传4:<input type="file" name="PicPath[]" /><br>';

$Str.='文件上传5:<input type="file" name="PicPath[]" /><br>';

$Str.='文件上传6:<input type="file" name="PicPath[]" /><br>';

$Str.='<input type="submit" value="提交确认">';

$Str.='</form>';

echo $Str;

?>

文件名:uploadfile.php

<?php

$State=isset($_GET['Send'])?trim($_GET['Send']):'';

if($State=='More')

{

require('./uploadfilemore.php');

//$FileData;//定义客户所上传的文件来源

$AffirmExtension=array('jpg','jpeg','gif','png');//定义我们所确认的有效扩展名有哪些

$UpFileSize=500*1024;//定义我们所上传文件的大小对其约束

$UpSeat='./uploadfile/';//定义上传文件的具体位置

$UpSubDir='images/';//定义上传文件的子目录

$UpLoadMore=new UpLoadMore($_FILES['PicPath'],$AffirmExtension,$UpFileSize,$UpSeat,$UpSubDir);

$Result=$UpLoadMore->FileUpLoad();

print_r($Result);

}

?>

文件名:uploadfilemore.php

<?php

/*

文件上传时需要注意的具体事项有:

1.文件扩展名

2.文件尺寸

3.上传的位置

4.上传的子目录

5.上传的文件名字

6.客户所上传的文件数据来源

*/

class UpLoadMore

{

private $FileData;//定义客户所上传的文件来源

private $AffirmExtension;//定义我们所确认的有效扩展名有哪些

private $UpFileSize;//定义我们所上传文件的大小对其约束

private $UpSeat;//定义上传文件的具体位置

private $UpSubDir;//定义上传文件的子目录

private $ValidFileData;//定义客户所上传的有效的文件数据

private $SelectUPLoadState;//请客户选择来确认是否一有错则全部不上传1,还是可以部分上传2

public function __construct($FileData,$AffirmExtension,$UpFileSize,$UpSeat,$UpSubDir,$SelectUPLoadState=2)

{

$this->FileData=$FileData;

$this->AffirmExtension=$AffirmExtension;

$this->UpFileSize=$UpFileSize;

$this->UpSeat=$UpSeat;

$this->UpSubDir=$UpSubDir;

$this->SelectUPLoadState=$SelectUPLoadState;

}

//外部调用返回结果

public function FileUpLoad()

{

//验证当前是否有文件上传

if(!$this->CheckFileIsNull())

{

$Back=array();

$Back['ErrorNO']=1;

return $Back;

}

//请客户选择来确认是否一有错则全部不上传1,还是可以部分上传2

if($this->SelectUPLoadState==1)

{

//验证当前客户所上传的图片扩展名是否有效

$FileExtension=$this->CheckExtensionIsValid();

if(!empty($FileExtension))

{

return $FileExtension;

}

//验证当前客户所上传的文件大小是否合法

$FiliSize=$this->CheckFileSizeIsValid();

if(!empty($FiliSize))

{

return $FiliSize;

}

}

elseif($this->SelectUPLoadState==2)

{

//验证当前客户所上传的图片扩展名是否有效

$FileExtension=$this->CheckExtensionIsValid();

//验证当前客户所上传的文件大小是否合法

$FiliSize=$this->CheckFileSizeIsValid();

}

//文件上传保存

$FileSaveState=$this->MoveFileSave($FileExtension,$FiliSize);

if(!empty($FileSaveState))

{

return $FileSaveState;

}

}

//验证当前客户所上传的文件是否为空

private function CheckFileIsNull()

{

if(empty($this->FileData) || !is_array($this->FileData))

{

return false;

}

$ErrorStateCount=0;

foreach ($this->FileData['error'] as $key => $value) {

if($value==4)

{

$ErrorStateCount++;

}

}

if($ErrorStateCount==count($this->FileData['error']))

{

return false;

}

//设置客户所选择的多文件,对其数组进行整理

$this->SetFileData();

return true;

}

//设置客户所选择的多文件,对其数组进行整理

private function SetFileData()

{

$FileArr=array();$DelSeat=array();

//整理出文件上传所有数组结构

foreach ($this->FileData as $key => $value) {

foreach ($value as $key2 => $value2) {

$FileArr[$key2][$key]=$value2;

}

}

//整理出没有上传的空文件所对应的索引

foreach($this->FileData['error'] as $key => $value) {

if($value==4)

{

$DelSeat[]=$key;

}

}

//删除没有上传的空文件数据

if(!empty($DelSeat))

{

foreach ($DelSeat as $key => $value) {

unset($FileArr[$value]);

}

}

$this->ValidFileData=$FileArr;

return true;

}

//获取当前文件的扩展名

private function GetCurrentFileExtension($Data)

{

$FileName=$Data['name'];

$FileArr=explode('.',$FileName);

return end($FileArr);

}

//验证当前客户所上传的图片扩展名是否有效

private function CheckExtensionIsValid()

{

$Back=array();

if(!empty($this->ValidFileData))

{

foreach ($this->ValidFileData as $key => $value) {

$Extension=$this->GetCurrentFileExtension($value);

if(!in_array($Extension,$this->AffirmExtension))

{

$Back['Data'][$key]=$value['name'];

}

}

}

if(!empty($Back))

{

$Back['ErrorNO']=2;

}

return $Back;

}

//验证当前客户所上传的文件大小是否合法

private function CheckFileSizeIsValid()

{

$Back=array();

if(!empty($this->ValidFileData))

{

foreach ($this->ValidFileData as $key => $value) {

if($value['size'] > $this->UpFileSize)

{

$Back['Data'][$key][]=$value['name'];

$Back['Data'][$key][]=$value['size'];

}

}

}

if(!empty($Back))

{

$Back['ErrorNO']=3;

}

return $Back;

}

//验证当前的目录是否是合法有效的

private function CheckDirIsValid()

{

if(is_dir($this->UpSeat))

{

return true;

}

@mkdir($this->UpSeat);//创建根目录

chmod($this->UpSeat,0777);//设置当前所创建的文件夹权限为可读可写

}

//验证当前目录下的子目录是否是合法有效的

private function CheckSubDirIsValid()

{

if(!empty($this->UpSubDir))

{

if(is_dir($this->UpSeat.$this->UpSubDir))

{

return true;

}

@mkdir($this->UpSeat.$this->UpSubDir);//创建根目录

chmod($this->UpSeat.$this->UpSubDir,0777);//设置当前所创建的文件夹权限为可读可写

}

return '';

}

//依据当前文件夹再创建出年月日的子文件夹

private function CheckYMDDirIsValid()

{

$Path=$this->UpSeat.$this->UpSubDir.date('Ymd');

if(is_dir($Path))

{

return $Path;

}

@mkdir($Path);//创建根目录

chmod($Path,0777);//设置当前所创建的文件夹权限为可读可写

return $Path;

}

//移动客户所上传的文件

private function MoveFileSave($FileExtension='',$FiliSize='')

{

$this->CheckDirIsValid();//验证当前的目录是否是合法有效的

$this->CheckSubDirIsValid();//验证当前目录下的子目录是否是合法有效的

$Back=array();

if(!empty($FileExtension) && $FileExtension['ErrorNO']==2)

{

foreach ($FileExtension['Data'] as $key => $value) {

unset($this->ValidFileData[$key]);

}

$Back['Addition']['Extension']['ErrorNO']=2;

$Back['Addition']['Extension']['Data']=$FileExtension['Data'];

}

if(!empty($FiliSize) && $FiliSize['ErrorNO']==3)

{

foreach ($FiliSize['Data'] as $key => $value) {

unset($this->ValidFileData[$key]);

}

$Back['Addition']['Size']['ErrorNO']=3;

$Back['Addition']['Size']['Data']=$FiliSize['Data'];

}

//print_r($this->ValidFileData);

if(!empty($this->ValidFileData))

{

foreach ($this->ValidFileData as $key => $value) {

if(is_uploaded_file($value['tmp_name']))

{

//获取当前文件扩展名

$Extension=$this->GetCurrentFileExtension($value);

//依据当前文件夹再创建出年月日的子文件夹

$Path=$this->CheckYMDDirIsValid().'/'.date('YmdHis').'_'.mt_rand(100,999).'.'.$Extension;

if(move_uploaded_file($value['tmp_name'],$Path))

{

$Back['ErrorNO']=0;

$Back['Data'][$key]=$Path;

}else{

$Back['ErrorNO']=4;

$Back['Data'][$key]=$value['name'];

}

}else{

$Back['ErrorNO']=4;

$Back['Data'][$key]=$value['name'];

}

}

}

return $Back;

}

}

?>

推荐阅读