A-A+

php多文件多图片的上传类

2012年05月20日 PHP技术文章 暂无评论 阅读 56 views 次

在php应用中,经常会遇到要求上传多文件或者是多图片,下面是笔者搜索的一款多文件多图片的上传类,整体源码不算很复杂,可以直接调用,可设置保存的路径,上传文件的类型,如果只是文件,可设置成图片格式,比如 gif,jpg,png等格式,也可以进行研究学习,下面有调用的代码。

<!--?php <br ?--> /**
* 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-&gt;user_post_file=$file;
$this-&gt;save_file_path=$path;
$this-&gt;max_file_size=$size;

if (!$type='')
{
$this-&gt;allow_type[]=$type;
}
}

/**
* upload files
*
* @access public
* @return int
*/
function upload()
{
for ($i=0;$i&lt;count($this-&gt;user_post_file['name']);$i++)
{
if ($this-&gt;user_post_file['error'][$i]==0)//Upload file status is normal
{
/*
Get the files's name,temp name,size,type and extension
*/
$name=$this-&gt;user_post_file['name'][$i];
$tmp_name=$this-&gt;user_post_file['tmp_name'][$i];
$size=$this-&gt;user_post_file['size'][$i];
$type=$this-&gt;user_post_file['type'][$i];
$ext_name=$this-&gt;getExtName($name);

/*
filesize
*/
if (!$this-&gt;checkSize($size))
{
$this-&gt;error_info[]='Length of '.$name.' is out of the value of upload_max_filesize!';
continue;
}

/*
extension name
*/
if (!$this-&gt;checkType($ext_name))
{
$this-&gt;error_info[]='Type of '.$name.' is illegal type!';
continue;
}

/*
illegal upload
*/
if (!is_uploaded_file($tmp_name))
{
$this-&gt;error_info[]=$name.'has been broken up!';
continue;
}

$base_name=$this-&gt;getBaseName($name,".".$ext_name);
$final_file_name=$base_name.'_'.time().'_'.rand(1,999999).'.'.$ext_name;
$this-&gt;final_file_path=$this-&gt;save_file_path.'/'.$final_file_name;

if (!move_uploaded_file($tmp_name,$this-&gt;final_file_path))
{
$this-&gt;error_info=$this-&gt;user_post_file['error'][$i];
continue;
}

$this-&gt;save_info[]=array(
"name"=&gt;$name,
"ext_name"=&gt;$ext_name,
"type"=&gt;$type,
"size"=&gt;$size,
"final_file_name"=&gt;$final_file_name,
"final_file_path"=&gt;$this-&gt;final_file_path
);
}
}
return count($this-&gt;save_info);
}

/**
* Check the file size is legitimate
*
* @param integer $szie
* @access private
* @return boolean
*/
private function checkSize($size)
{
if ($size&gt;$this-&gt;max_file_size)
{
return false;
}
else
{
return true;
}
}

/**
* Check the legality of file types
*
* @access private
* @return boolean
*/
private function checkType($extension)
{
foreach($this-&gt;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-&gt;error_info)!=0)
{
foreach ($this-&gt;error_info as $k=&gt;$v)
{
echo ($k+1),':',$v,'
';
}
}
}

/**
* Get some file information useful
*
* @access public
* @return Array
*/
function getSaveInfo()
{
return $this-&gt;save_info;
}
} ?&gt;

调用方法示例

$uploads = $_FILES['file'];
$num_file = count($uploads['name']);

$up = new UploadFile($uploads,'test_upload',102400);
$num = $up-&gt;upload();
$info=$up-&gt;getSaveInfo();

if($num == $num_file )
{
echo $num,'个文件均上传成功';
print_r($info);
exit;
}
else
{
echo $num,'个文件上传成功
';
print_r($info);
echo $up-&gt;showErrorInfo();
exit;
}

给我留言