php 图片文件上传类

来源:互联网 发布:中国环保网络电视台 编辑:程序博客网 时间:2024/05/19 19:34
<?phpclass UploadClass{protected $allow_type = array('gif', 'png', 'jpg','jpeg'); //设置限制上传文件的类型protected $max_size = 2097152;           //限制文件上传大小2M(字节)protected $path = null;           //上传文件保存的路径    protected $origin_name= null;              //源文件名    protected $tmp_file_name= null;               //临时文件名    protected $file_type= null;               //文件类型(文件后缀)    protected $file_size= null;              //文件大小    protected $new_file_name= null;              //新文件名    protected $error_id = null;              //错误号    protected $error= null;              //错误报告消息      public function __construct($path = './upload', $max_size = 2097152){$this->path= $path;$this->max_size= $max_size;} /**     * 用于设置成员属性($path, $allowtype,$maxsize)     * 可以通过连贯操作一次设置多个属性值     *@param  string $key  成员属性名(不区分大小写)     *@param  mixed  $val  为成员属性设置的值     *@return  object     返回自己对象$this,可以用于连贯操作     */ public function set($key, $val){$key = strtolower($key); if( array_key_exists( $key, get_class_vars(get_class($this) ) ) ){$this->$key =  $val;}return $this;}/* 设置上传出错信息 */    public function get_error() {      $str = "上传文件<font color='red'>{$this->origin_name}</font>时出错 : ";      switch ($this->error_id) {        case 4: $str .= "没有文件被上传"; break;        case 3: $str .= "文件只有部分被上传"; break;        case 2: $str .= "上传文件的大小超过了指定大小"; break;        case 1: $str .= "上传的文件超过了最大限制的值"; break;        case -1: $str .= "未允许类型"; break;        case -2: $str .= "文件过大,上传的文件不能超过{$this->maxsize}个字节"; break;        case -3: $str .= "上传失败"; break;        case -4: $str .= "建立存放上传文件目录失败,请重新指定上传目录"; break;        case -5: $str .= "必须指定上传文件的路径"; break;case -6: $str .= "上传表单名不存在"; break;        default: $str .= "未知错误";      }      return $str.'<br>';    }/* 检查是否有存放上传文件的目录 */    protected function check_file_path() {$path = $this->path;if(empty($path)){$this->set('error_id', -5);return false;}if (!file_exists($this->path) || !is_writable($this->path)) {if (!@mkdir($this->path, 0755, true)) {$this->set('error_id', -5);return false;}}return true;    }public function upload($fileField, $new_file_name = null){   if( !isset($_FILES[$fileField]) ){$this->set('error_id',-6);$this->set('error',$this->get_error());return false;}//将文件上传的信息取出赋给变量$name = $_FILES[$fileField]['name'];$tmp_name = $_FILES[$fileField]['tmp_name'];$size = $_FILES[$fileField]['size'];$error = $_FILES[$fileField]['error'];$return = true;if( !empty($name) ){/* 设置文件信息 */if($this->set_files($name,$tmp_name,$size,$error)) {  /* 上传之前先检查一下大小和类型 */  if($this->check_file_size() && $this->check_file_type()){ /* 为上传文件设置新文件名 */$this->set_new_file_name($new_file_name); /* 上传文件  返回0为成功, 小于0都为错误 *///检查文件路径是滞合法 if( !$this->check_file_path() ) {       $this->set('error',$this->get_error());return false;}if($this->move_File()){   return true;}else{  $return=false;}  }else{$return=false;}} else {  $return=false; }        //如果$return为false, 则出错,将错误信息保存在属性errorMess中        if(!$return)          $this->error = $this->get_error();            return $return;      }  }//判断是不是png gif jpg 图片public function is_images($file_name){$images_type = array('gif','jpeg','png','jpg');$extension = pathinfo($file_name,PATHINFO_EXTENSION);if (in_array($extension,$images_type)) {//后缀是图片list($width, $height, $type, $attr) = getimagesize($file_name);if (in_array($type,$images_type)) {//确定其内容是真正的图片return true;}}return false;}public function get_random_name($extend) {          $file_name = date('YmdHis')."_".rand(1000,0999);          return $file_name.'.'.$extend;     }public function get_extend($file_name){$extend = pathinfo($file_name);$extend = strtolower($extend["extension"]);return $extend;}/**      * 获取上传后的文件名称     * @param  void   没有参数     * @return string 上传后,新文件的名称, 如果是多文件上传返回数组     */    public function get_file_name(){return $this->new_file_name;    }          /* 设置和$_FILES有关的内容 */    protected function set_files($name="", $tmp_name="", $size=0, $error=0) {$this->set('error_id', $error);if($error) return false;$this->set('origin_name', $name);$this->set('tmp_file_name',$tmp_name);$this->set('file_type', $this->get_extend($name));$this->set('file_size', $size);return true;    }        //设置上传后的文件名称    protected function set_new_file_name($file_name = null) {if( $file_name == null)$this->set('new_file_name', $this->get_random_name( $this->file_type)); else $this->set('new_file_name', $file_name);    }      //检查上传的文件是否是合法的类型    protected function check_file_type() {if ( !in_array(strtolower( $this->file_type), $this->allow_type)){$this->set('error_id', -1);return false;}return true;    }      //检查上传的文件是否是允许的大小    protected function check_file_size() {if ($this->file_size > $this->max_size) {$this->set('error_id', -2);return false;}return true;    }          //复制上传文件到指定的位置protected function move_file(){if( $this->error_id == null ) {$path = rtrim($this->path, '/').'/';$path .= $this->new_file_name;if (@move_uploaded_file($this->tmp_file_name, $path)) {return true;}else{  $this->set('error_id', -3);  return false;}} return false;}}



参考了网上的资料写的

0 0
原创粉丝点击