多文件上传
<meta charset="utf-8"/>
<?php
class upload
{
public $files;
public $seterror;
public $allowtype;
public $filetype;
public $typeerror;
public $path;
public $succ=0;
public $filenewname;
function __construct($files,$path)
{
$this->path=$path;
$this->files=$files;
$this->seterror=1;
$this->allowtype=array("jpg","gif","JPG","png");
$this->filetype=array();
$this->filenewname=array();
if(is_array($_FILES[$files][‘name‘]))
{
foreach($_FILES[$files][‘name‘] as $key => $names)
{
$file_ex=explode(".",$names);//分成数组
$this->filetype[$key]=$file_ex[count($file_ex)-1];
$this->filenewname[$key]=$this->path.time().$key.".".$this->filetype[$key];
}
foreach($this->filetype as $key => $type)
{
if(!in_array($type,$this->allowtype))
{
$this->typeerror=$key+1;
$this->seterror="第".$this->typeerror."张图片类型不符合要求";
echo $this->seterror;
exit;
}
}
foreach($_FILES[$files][‘error‘] as $val)
{
if($val!=0)
{
switch($val)
{
case 1:;
case 2:$this->seterror="上传的文件过大,最大能上传2M";break;
case 3:$this->seterror="文件只有部分被上传";break;
case 4:$this->seterror="文件没有被上传";break;
case 6:$this->seterror="找不到临时文件夹";break;
case 7:$this->seterror="文件写入失败";break;
}
echo $this->seterror;
exit;
}
}
}
if(!file_exists($path))
{
mkdir($path,0777);
}
if($this->seterror==1)
{
$this->move_file();
}
}
private function move_file()
{
foreach($_FILES[$this->files][‘tmp_name‘] as $key => $tmp)
{
if(move_uploaded_file($tmp,$this->filenewname[$key]))
{
$this->succ+=1;
}
}
if($this->succ==count($_FILES[$this->files][‘name‘]))
{
echo "全部上传成功";
}
}
}
?>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。