加入收藏 | 设为首页 | 会员中心 | 我要投稿 鹰潭站长网 (https://www.0701zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

一个非常实用的php文件上传类

发布时间:2021-02-05 07:26:45 所属栏目:PHP教程 来源:网络整理
导读:其实网上已经有很多这样的类了,不过出于练手的目的还是自己仿照着写了一个。 下面的代码放在一个名为UploadFile.class.php文件内 /** * 构造函数,初始化 * @param string $rand_name 是否随机命名 * @param string $save_path 文件保存路径 * @param strin
副标题[/!--empirenews.page--]

其实网上已经有很多这样的类了,不过出于练手的目的还是自己仿照着写了一个。

下面的代码放在一个名为UploadFile.class.php文件内

/** * 构造函数,初始化 * @param string $rand_name 是否随机命名 * @param string $save_path 文件保存路径 * @param string $allow_type 允许上传类型 $allow_type可为数组 array('jpg','jpeg','png','gif'); $allow_type可为字符串 'jpg|jpeg|png|gif';中间可用' ',',';','|'分割 */ public function __construct($rand_name=true,$save_path='./upload/',$allow_type=''){ $this->rand_name = $rand_name; $this->save_path = $save_path; $this->allow_type = $this->get_allow_type($allow_type); } /** * 上传文件 * 在上传文件前要做的工作 * (1) 获取文件所有信息 * (2) 判断上传文件是否合法 * (3) 设置文件存放路径 * (4) 是否重命名 * (5) 上传完成 * @param array $file 上传文件 * $file须包含$file['name'],$file['size'],$file['error'],$file['tmp_name'] */ public function upload_file($file){ //$this->file = $file; $this->file_name = $file['name']; $this->file_size = $file['size']; $this->error = $file['error']; $this->file_tmp_name = $file['tmp_name']; $this->ext = $this->get_file_type($this->file_name); switch($this->error){ case 0: $this->msg = ''; break; case 1: $this->msg = '超出了php.ini中文件大小'; break; case 2: $this->msg = '超出了MAX_FILE_SIZE的文件大小'; break; case 3: $this->msg = '文件被部分上传'; break; case 4: $this->msg = '没有文件上传'; break; case 5: $this->msg = '文件大小为0'; break; default: $this->msg = '上传失败'; break; } if($this->error==0 && is_uploaded_file($this->file_tmp_name)){ //检测文件类型 if(in_array($this->ext,$this->allow_type)==false){ $this->msg = '文件类型不正确'; return false; } //检测文件大小 if($this->file_size > $this->max_size){ $this->msg = '文件过大'; return false; } } $this->set_file_name(); $this->uploaded = $this->save_path.$this->new_name; if(move_uploaded_file($this->file_tmp_name,$this->uploaded)){ $this->msg = '文件上传成功'; return true; }else{ $this->msg = '文件上传失败'; return false; } } /** * 设置上传后的文件名 * 当前的毫秒数和原扩展名为新文件名 */ public function set_file_name(){ if($this->rand_name==true){ $a = explode(' ',microtime()); $t = $a[1].($a[0]*1000000); $this->new_name = $t.'.'.($this->ext); }else{ $this->new_name = $this->file_name; } } /** * 获取上传文件类型 * @param string $filename 目标文件 * @return string $ext 文件类型 */ public function get_file_type($filename){ $ext = pathinfo($filename,PATHINFO_EXTENSION); return $ext; } /** * 获取可上传文件的类型 */ public function get_allow_type($allow_type){ $s = array(); if(is_array($allow_type)){ foreach($allow_type as $value){ $s[] = $value; } }else{ $s = preg_split("/[s,|;]+/",$allow_type); } return $s; } //获取错误信息 public function get_msg(){ return $this->msg; }

}
?>

其实上面的代码中还有一个可以改进的地方,就是将那些以‘file_'开头的变量缩写为一个$file数组,这样感觉更好一些。

下面我们来测试一下上面的代码。我在一个名为upfile.php文件写测试代码,同时将UploadFile.class.php放在同一个路径下。

upfile 0){ echo "Error: " . $_FILES["file"]["error"] . "
"; }else{ $file = $_FILES['file'];
    $upload = new UploadFile(true,'./images/',array('jpg','png'));
    $upload->upload_file($file);
    echo $upload->get_msg();
  }
}else{

?>
<form action="" method='post' enctype="multipart/form-data">

在上面的代码中,我们可以尝试修改第15行的参数,用来判断一下我们写的方法是否正确。

(编辑:鹰潭站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读