栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php面向对象之文件上传类

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php面向对象之文件上传类

//文件上传类(功能)

class FileUpload{

    protected $upfile;

    protected $path;

    protected $error;

    protected $fileinfo;

    protected $typelist=array();

    protected $maxsize=0;

    public function __construct($name,$path){

        $this->upfile=$_FILES[$name];

        $this->path=rtrim($path,"/")."/";

       

    }

       

    public function __set($param,$value){

        $this->$param=$value;

    }

       

    public function __get($param){

        return $this->$param;

    }

       

    //验证上传的错误号

    private function checkError(){

        if($this->upfile['error']>0){

            switch($this->upfile['error']){

                case 1: $info="上传文件大小超出PHP配置文件的设置"; break;

                case 2: $info="上传文件大小超过了表单中MAX_FILE_SIZE指定的值"; break;

                case 3: $info="文件只有部分被上传。"; break;

                case 4: $info="没有文件被上传。"; break;

                case 6: $info="找不到临时文件夹。"; break;

                case 7: $info="文件写入失败。"; break;

                default: $info="未知错误"; break;

            }

            $this->error=$info;

            return false;

        }

        return true;

    }

       

    //验证上传类型

    private function checkType(){

        if(count($this->typelist)>0){

            if(!in_array($this->upfile['type'],$this->typelist)){

                $this->error="上传文件类型错误:".$upfile['type'];

                return false;

            }

        }

        return true;

    }

       

    //验证大小

    private function checkSize(){

        if($this->maxsize>0 && $this->upfile['size']>$this->maxsize){

            $this->error="上传文件大小超出了允许范围!".$maxsize;

            return false;

        }

        return true;

    }

       

    //处理上传的文件名

    private function makeName(){

        do{

        //随机一个文件名,格式:时间戳+4位随机数+源后缀名

        $newname = time().rand(1000,9999).".".pathinfo($this->upfile['name'],PATHINFO_EXTENSION);

        }while(file_exists($this->path.$newname)); //判断随机的文件名是否存在。

        $this->fileinfo['savename']=$newname;

        return true;

    }

       

    //移动上传文件

    private function movefile(){

        if(is_uploaded_file($this->upfile['tmp_name'])){

            if(move_uploaded_file($this->upfile['tmp_name'],$this->path.$this->fileinfo['savename'])){

                return true;

            }else{

                $this->error='执行上传文件移动失败!';

            }

        }else{

            $this->error='不是一个有效的上传文件!';

        }

        return false;

    }

    //执行上传

    public function upload(){

        return ($this->checkError() && $this->checkType() && $this->checkSize() && $this->makeName() && $this->movefile());

    }

       

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/229683.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号