*/
class upfile {
public $fcount = 1; //上传文件的数量
public $ftype = array('jpg','jpeg','gif','png'); //文件格式
public $fsize = 1024; //文件大小单位kb
public $fdir = 'www.111com.net/'; //文件存放目录
public $errormsg = ''; //产生的临时错误信息
/**
*函数名:get_tmp_file($putfile)
*作用:取得上传的临时文件名
*@param array $putfile
*@return string $upimg 返回临时文件名
*/
function get_tmp_file($putfile){
if($this->fcount == 1){
$tmpfile = $putfile['tmp_name'];
}else{
for($i=0;$i<$this->fcount;$i++){
$tmpfile[] = $putfile['tmp_name'][$i];
}
}
return $tmpfile;
}
/**
*函数名:get_suffix($filename)
*作用:取得文件的后缀名
*@param file $filename
*@return string $suffixname 返回后缀名
*/
function get_suffix($filename){
$link = pathinfo($filename);
$suffixname = strtolower($link['extension']);
return $suffixname;
}
/**
*验证文件大小
*@author 赵红健
*@param $filesize
*@return booln
*/
function check_file_size($filesize){
$this->errormsg = '';
if($filesize/1000 > $this->fsize){
$this->errormsg = '警告:文件超出大小!';
return false;
}else{
return true;
}
}
/**
*验证文件类型是否合法
*@author 赵红健
*@param $filesuffix
*@return booln
*/
function check_file_suffix($filesuffix){
$this->errormsg = '';
if(!in_array($filesuffix,$this->ftype)){
$this->errormsg = '警告:文件类型不在允许范围内!';
return false;
}else{
return true;
}
}
/**
*移动临时文件
*@author 赵红健
*@param $filesuffix
*@return booln
*/
function move_temp_file($tmpfile,$targetfile){
$this->errormsg = '';
if(!move_uploaded_file($tmpfile,$targetfile)){
$this->errormsg = '警告:文件移动失败!';
return false;
}else{
return true;
}
}
/**
*函数名:update_file($putfile)
*作用:上传文件
*@param array $putfile
*@return array 文件信息
*/
function update_file($putfile){
$tmpfile = $this->get_tmp_file($putfile);
if(!file_exists($this->fdir)){
$this->errormsg[] = '错误:目录'.$this->fdir.'不存在';
return $this->errormsg;
}
$this->fdir = substr($this->fdir,strlen($this->fdir)-1,1)=='/'?$this->fdir:$this->fdir.'/';
if(!is_array($putfile['size'])){
$fileinfo['file_size'] = $putfile['size'];
if(!$this->check_file_size($fileinfo['file_size'])){
$fileinfo['error'] = $this->errormsg;
return $fileinfo;
}
$fileinfo['file_suffix'] = $this->get_suffix($putfile['name']);
if(!$this->check_file_suffix($fileinfo['file_suffix'])){
$fileinfo['error'] = $this->errormsg;
return $fileinfo;
}
$fileinfo['file_name'] = date('ymdhms').'.'.$fileinfo['file_suffix'];
if(!$this->move_temp_file($tmpfile,$this->fdir.$fileinfo['file_name'])){
$fileinfo['error'] = $this->errormsg;
return $fileinfo;
}
return $fileinfo;
}else{
for($i=0;$i<$this->fcount;$i++){
$fileinfo[$i]['file_size'] = $putfile['size'][$i];
if(!$this->check_file_size($fileinfo[$i]['file_size'])){
$fileinfo[$i]['error'] = $this->errormsg;
continue;
}
$fileinfo[$i]['file_suffix'] = $this->get_suffix($putfile['name'][$i]);
if(!$this->check_file_suffix($fileinfo[$i]['file_suffix'])){
$fileinfo[$i]['error'] = $this->errormsg;
continue;
}
$fileinfo[$i]['file_name'] = date('ymdhms').rand().'.'.$fileinfo[$i]['file_suffix'];
if(!$this->move_temp_file($tmpfile[$i],$this->fdir.$fileinfo[$i]['file_name'])){
$fileinfo[$i]['error'] = $this->errormsg;
continue;
}
}
return $fileinfo;
}
}
}
?>