PHP头条
热点:

php中实用的文件上传类


class upload{
    /**
     * html表单 input 域的 name 属性,默认为'file'
    */
    var $file_field = 'file' ;
   
    /**
     * $_files 数组
    */
    var $file_array;
   
    /**
     * 保存路径,默认为当前路径
    */
    var $save_path = '';
   
    /**
     * 自定义文件名
    */
    var $define_name;   
   
    /**
     * 最终保存的文件名
    */
    var $name;
   
    /**
     * 文件大小,单位:字节
    */
    var $size;
   
    /**
     * 文件扩展名,不包含"."
    */
    var $ext;    
   
    /**
     * 允许上传的文件类型,默认不限制
    */
    var $allow_ext = array();
   
    /**
     * 允许上传的文件类型,默认不限制
    */
    var $allow_size = false;
   
    /**
     * 如果已存在同名文件,是否允许覆盖,默认不覆盖。
    */
    var $overwrite = false;
   
    /**
     * 错误提示
    */
    var $error_code;
   
    /**
     * 构造函数
    */
    public function __construct(){
        if(!is_uploaded_file($_files[$this->file_field]['tmp_name'])){
            die("非法上传!");
        }else{
            $this->file_array = $_files[$this->file_field];
            $this->name = $this->getpro('name');
            $this->size = $this->getpro('size');
            $this->ext = $this->getpro('ext');   
        }
    }
   
   
    /**
     * 上传操作函数
     * @abstract    上传成功返回 true , 否则返回相应错误代码
     * @return        string or bool
    */
    public function upload(){
        if(is_uploaded_file($this->file_array['tmp_name'])){   
               
            if(!empty($this->allow_ext)){
                if(!in_array($this->ext,$this->allow_ext)){
                    $this->error_code = "不允许上传该类型文件!";   
                    return false;
                }
            }
           
            if(!@file_exists(iconv('utf-8','gbk',$this->save_path))){
                $this->error_code = "文件上传目录不存在!";
                return false;
            }
           
            if(!is_writable(iconv('utf-8','gbk',$this->save_path))){
                $this->error_code = "文件上传目录不可写入!";
                return false;
            }
           
            if($this->overwrite==false && @file_exists(iconv('utf-8','gbk',$this->save_path.$this->name))){
                $this->error_code = "该文件已存在!";
                return false;
            }
           
            if($this->allow_size){
                if($this->size > $this->allow_size){
                    $this->error_code = "文件大小超过系统限制!";
                    return false;
                }
            }
           
            $result = @move_uploaded_file($this->file_array['tmp_name'],iconv("utf-8","gbk",$this->save_path.$this->getpro("name")));           
            if($result){
                return true;
            }else{
                switch($this->file_array['error']){
                    case 1:
                        $this->error_code = "上传的文件超过了 upload_max_filesize 选项限制的值!";
                    break;
                    case 2:
                        $this->error_code = "上传文件的大小超过了 max_file_size 选项指定的值!";
                    break;
                    case 3:
                        $this->error_code = "上传过程被中断!";
                    break;
                    case 4:
                        $this->error_code = "没有文件被上传!";
                    break;
                    case 6:
                        $this->error_code = "找不到临时文件夹!";
                    break;
                    case 7:
                        $this->error_code = "文件写入失败!";
                    break;
                }
                return false;
            }
        }
    }
   
    /**
     * 上传操作函数
     * @abstract    获取文件属性
     * @param        $itme:string类型,有效输入为name(文件名)、ext(文件扩展名)、size(文件大小)
     * @return        string
    */
    public function getpro($item){
        switch($item){
            case "name":
                $filename = $this->file_array['name'];
                return isset($this->define_name) ? $this->define_name.".".$this->ext : $filename;
            break;   
            case "ext":
                $filename = $this->file_array['name'];
                $filter = explode(".",$filename);
                return strtolower($filter[count($filter)-1]);
            break;
            case "size":
                return $this->file_array['size'];
            break;
            default:
                die("非法操作!");
            break;
        }
    }
}

www.phpzy.comtrue/php/22646.htmlTechArticlephp中实用的文件上传类 class upload{ /** * html表单 input 域的 name 属性,默认为'file' */ var $file_field = 'file' ; /** * $_files 数组 */ var $file_array; /** * 保存路径,默认为当前路径 */ var $save_path = '...

相关文章

    暂无相关文章

PHP之友评论

今天推荐