文件名: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,$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;
}
}
必修>