php文件上传类程序代码
我们现在只要搜索文件上传类有大把,但是真正好用的上传类不多,下面我介绍这个文件上传类是我自己使用了很久,非常不错的一个代码,大家可参考参考.
php文件上传类程序代码如下:
- <?php
-
-
-
- class uploadFile {
-
- public $max_size = '1000000';
- public $file_name = 'date';
- public $allow_types;
- public $errmsg = '';
- public $uploaded = '';
- public $save_path;
- private $files;
- private $file_type = array();
- private $ext = '';
-
-
-
-
-
-
-
- public function __construct($save_path = './upload/',$file_name = 'date',$allow_types = '') {
- $this->file_name = $file_name;
- $this->save_path = (preg_match('//$/',$save_path)) ? $save_path : $save_path . '/';
- $this->allow_types = $allow_types == '' ? 'jpg|gif|png|zip|rar' : $allow_types;
- }
-
-
-
-
-
-
-
- public function upload_file($files) {
- $name = $files['name'];
- $type = $files['type'];
- $size = $files['size'];
- $tmp_name = $files['tmp_name'];
- $error = $files['error'];
-
- switch ($error) {
- case 0 : $this->errmsg = '';
- break;
- case 1 : $this->errmsg = '超过了php.ini中文件大小';
- break;
- case 2 : $this->errmsg = '超过了MAX_FILE_SIZE 选项指定的文件大小';
- break;
- case 3 : $this->errmsg = '文件只有部分被上传';
- break;
- case 4 : $this->errmsg = '没有文件被上传';
- break;
- case 5 : $this->errmsg = '上传文件大小为0';
- break;
- default : $this->errmsg = '上传文件失败!';
- break;
- }
- if($error == 0 && is_uploaded_file($tmp_name)) {
-
- if($this->check_file_type($name) == FALSE){
- return FALSE;
- }
-
- if($size > $this->max_size){
- $this->errmsg = '上传文件<font color=red>'.$name.'</font>太大,最大支持<font color=red>'.ceil($this->max_size/1024).'</font>kb的文件';
- return FALSE;
- }
- $this->set_save_path();
- $new_name = $this->file_name != 'date' ? $this->file_name.'.'.$this->ext : date('YmdHis').'.'.$this->ext;
- $this->uploaded = $this->save_path.$new_name;
-
- if(move_uploaded_file($tmp_name,$this->uploaded)){
- $this->errmsg = '文件<font color=red>'.$this->uploaded.'</font>上传成功!';
- return TRUE;
- }else{
- $this->errmsg = '文件<font color=red>'.$this->uploaded.'</font>上传失败!';
- return FALSE;
- }
-
- }
- }
-
-
-
-
-
-
-
- public function check_file_type($filename){
- $ext = $this->get_file_type($filename);
- $this->ext = $ext;
- $allow_types = explode('|',$this->allow_types);
-
-
- if(in_array($ext,$allow_types)){
- return TRUE;
- }else{
- $this->errmsg = '上传文件<font color=red>'.$filename.'</font>类型错误,只支持上传<font color=red>'.str_replace('|',',',$this->allow_types).'</font>等文件类型!';
- return FALSE;
- }
- }
-
-
-
-
-
-
-
- public function get_file_type($filename){
- $info = pathinfo($filename);
- $ext = $info['extension'];
- return $ext;
- }
-
-
-
-
- public function set_save_path(){
- $this->save_path = (preg_match('//$/',$this->save_path)) ? $this->save_path : $this->save_path . '/';
- if(!is_dir($this->save_path)){
-
- $this->set_dir();
- }
- }
-
-
-
-
-
-
-
-
- public function set_dir($dir = null){
-
- if(!$dir){
- $dir = $this->save_path;
- }
- if(is_dir($dir)){
- $this->errmsg = '需要创建的文件夹已经存在!';
- }
- $dir = explode('/', $dir);
- foreach($dir as $v){
- if($v){
- $d .= $v . '/';
- if(!is_dir($d)){
- $state = mkdir($d, 0777);
- if(!$state)
- $this->errmsg = '在创建目录<font color=red>' . $d . '时出错!';
- }
- }
- }
- return true;
- }
- }
-
-
-
-
-
-
-