您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php中实用的文件上传类

2024/3/20 11:49:26发布60次查看
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;        }    }}
文章地址:
转载随意^^请带上本文地址!
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product