php多文件多图片的上传类
在php应用中,经常会遇到要求上传多文件或者是多图片,下面是笔者搜索的一款多文件多图片的上传类,整体源码不算很复杂,可以直接调用,可设置保存的路径,上传文件的类型,如果只是文件,可设置成图片格式,比如 gif,jpg,png等格式,也可以进行研究学习,下面有调用的代码。
[code lang="php"]
/**
* This class implements multiple file uploads for users to upload images
* @version 0.1.1 (build 10-11-2010)
*/
/*----------------------------------------------------*/
class UploadFile
{
private $user_post_file=array();
private $save_file_path='';
private $max_file_size='';
private $allow_type=array('gif','jpg','png','zip','rar','txt','doc','pdf');
private $final_file_path='';
private $save_info=array();
private $error_info=array();
/**
* Initialization information
*
* @param array $file
* @param string $path
* @param integer $size
* @param array $type
* @access public
* @return null
*/
function __construct($file,$path,$size=2097152,$type='')
{
$this->user_post_file=$file;
$this->save_file_path=$path;
$this->max_file_size=$size;
if (!$type='')
{
$this->allow_type[]=$type;
}
}
/**
* upload files
*
* @access public
* @return int
*/
function upload()
{
for ($i=0;$i<count($this->user_post_file['name']);$i++)
{
if ($this->user_post_file['error'][$i]==0)//Upload file status is normal
{
/*
Get the files's name,temp name,size,type and extension
*/
$name=$this->user_post_file['name'][$i];
$tmp_name=$this->user_post_file['tmp_name'][$i];
$size=$this->user_post_file['size'][$i];
$type=$this->user_post_file['type'][$i];
$ext_name=$this->getExtName($name);
/*
filesize
*/
if (!$this->checkSize($size))
{
$this->error_info[]='Length of '.$name.' is out of the value of upload_max_filesize!';
continue;
}
/*
extension name
*/
if (!$this->checkType($ext_name))
{
$this->error_info[]='Type of '.$name.' is illegal type!';
continue;
}
/*
illegal upload
*/
if (!is_uploaded_file($tmp_name))
{
$this->error_info[]=$name.'has been broken up!';
continue;
}
$base_name=$this->getBaseName($name,".".$ext_name);
$final_file_name=$base_name.'_'.time().'_'.rand(1,999999).'.'.$ext_name;
$this->final_file_path=$this->save_file_path.'/'.$final_file_name;
if (!move_uploaded_file($tmp_name,$this->final_file_path))
{
$this->error_info=$this->user_post_file['error'][$i];
continue;
}
$this->save_info[]=array(
"name"=>$name,
"ext_name"=>$ext_name,
"type"=>$type,
"size"=>$size,
"final_file_name"=>$final_file_name,
"final_file_path"=>$this->final_file_path
);
}
}
return count($this->save_info);
}
/**
* Check the file size is legitimate
*
* @param integer $szie
* @access private
* @return boolean
*/
private function checkSize($size)
{
if ($size>$this->max_file_size)
{
return false;
}
else
{
return true;
}
}
/**
* Check the legality of file types
*
* @access private
* @return boolean
*/
private function checkType($extension)
{
foreach($this->allow_type as $type)
{
if(strcasecmp($extension,$type) == 0)
{
return TRUE;
}
}
return FALSE;
}
/**
* Get the file extension
*
* @param string $file_name
* @access private
* @return string
*/
private function getExtName($file_name)
{
$p=pathinfo($file_name);
return $p['extension'];
}
/**
* Get the file name (without extension)
*
* @param string $file_name
* @param string $ext_name
* @access private
* @return string
*/
private function getBaseName($file_name,$ext_name)
{
$base_name=basename($file_name,$ext_name);
return $base_name;
}
/**
* show error message
*
* @access public
* @return null
*/
function showErrorInfo()
{
if (count($this->error_info)!=0)
{
foreach ($this->error_info as $k=>$v)
{
echo ($k+1),':',$v,'
';
}
}
}
/**
* Get some file information useful
*
* @access public
* @return Array
*/
function getSaveInfo()
{
return $this->save_info;
}
} ?>
[/code]
调用方法示例
[code lang="php"]
$uploads = $_FILES['file'];
$num_file = count($uploads['name']);
$up = new UploadFile($uploads,'test_upload',102400);
$num = $up->upload();
$info=$up->getSaveInfo();
if($num == $num_file )
{
echo $num,'个文件均上传成功';
print_r($info);
exit;
}
else
{
echo $num,'个文件上传成功
';
print_r($info);
echo $up->showErrorInfo();
exit;
}
[/code]