文件上传类demo
in 后端 with 1 comment

文件上传类demo

in 后端 with 1 comment

最近沉迷laravel的链式写法不能自拔,最近有个单子需要用到文件上传,于是为了以后考虑自写了一个操作类

默认配置下直接使用

    $file = new fileClass();
    $file->save($_FILES['file_demo']);

本来要做附件表的,这单子不值这功能,就没深度写了。这单做完再加上这功能。

支持外部修改 文件大小、格式限制、存储位置。由于深受laravel的链式思想茶毒,所以这些功能调用十分优雅???(黑人脸)

    //设置最大上传 2M
    $file->size(2)->save($_FILES['file_demo']);
    //设置只允许 jpg,png ,最大 5M
    $file->size(5)->allow(['jpg','png'])->save($_FILES['file_demo']);
    //设置存储到 /upload/img/ 目录
    $file->path('img/')->save($_FILES['file_demo']);

具体方法在注释里已经说的很清楚了,可以自行拓展开发...

注:文中用dirname()来定位文件夹,如果用在自己的项目,请按自己的目录结构设置dirname()

code:

    <?php
    /**
     * Created by PhpStorm.
     * User: 敲代码的小阿狸
     * Date: 2016/11/4
     */
    
    class fileClass {
        /*
        |--------------------------------------------------------------------------
        | 附件
        |--------------------------------------------------------------------------
        */
    
        private $file = [];
    
        /*
        |--------------------------------------------------------------------------
        | 附件存储位置
        |--------------------------------------------------------------------------
        */
    
        private $file_path = '/upload/';
    
        /*
        |--------------------------------------------------------------------------
        | 允许的附件格式
        |--------------------------------------------------------------------------
        */
    
        private $file_allow = ['jpg','png','bmp','jpeg','gif'];
    
        /*
        |--------------------------------------------------------------------------
        | 允许的附件大小
        |--------------------------------------------------------------------------
        */
    
        private $file_size = 0.5;
    
        /*
        |--------------------------------------------------------------------------
        | 是否开启附件表
        |--------------------------------------------------------------------------
        */
    
        private $file_log = false;
    
        /*
        |--------------------------------------------------------------------------
        | 数据表名称
        |--------------------------------------------------------------------------
        */
    
        private $file_table = 'files';
    
        /*
        |--------------------------------------------------------------------------
        | 构造方法
        |--------------------------------------------------------------------------
        */
    
        public function __construct(){
            if($this->file_log && $this->file_table != ""){
                //数据库操作
                //留坑
            }
        }
    
        /*
        |--------------------------------------------------------------------------
        | 检测文件拓展名是否允许
        |--------------------------------------------------------------------------
        */
    
        private function checkType(){
            $aryStr = explode('.' , $this->file['name']);
            $this->file['type'] = strtolower($aryStr[count($aryStr)-1]);
            if (in_array(strtolower($this->file['type']), $this->file_allow)) {
                return true;
            }else {
                return false;
            }
        }
    
        /*
        |--------------------------------------------------------------------------
        | 外部设置附件size
        |--------------------------------------------------------------------------
        | 单位(MB) 
        | $file->size('1')
        */
    
        public function size($max){
            $this->file_size = $max;
            return $this;
        }
    
        /*
        |--------------------------------------------------------------------------
        | 外部设置附件格式
        |--------------------------------------------------------------------------
        | 数组形式
        | allow(['jpg','png'])
        */
    
        public function allow($type){
            $this->file_allow = $type;
            return $this;
        }
    
        /*
        |--------------------------------------------------------------------------
        | 外部设定存储位置
        |--------------------------------------------------------------------------
        | 注:设定的位置根目录在网站的upload目录中
        | 如 path('bannar/') 则为 /upload/bannar/
        */
    
        public function path($path){
            $this->file_path .= $path;
            //若目录不存在则创建
            if(!file_exists(dirname(dirname(__FILE__)).$this->file_path)){
                mkdir(dirname(dirname(__FILE__)).$this->file_path);
            }
            return $this;
        }
    
        /*
        |--------------------------------------------------------------------------
        | 保存文件
        |--------------------------------------------------------------------------
        | 返回值为数组形式
        | success 为 false 时,error 为错误提示
        | success 为 true  时,url   为文件存储位置
        */
    
        public function save($files){
            $this->file['name'] = $files['name'];
            $this->file['tmp_name'] = $files['tmp_name'];
            $this->file['size'] = $files['size'];
            $this->file['error'] = $files['error'];
    
            $maxsize = $this->file_size * 1024 * 1024;
    
            if($this->file['size'] > $maxsize){
                return [
                    'success' => false,
                    'error' => '错误:文件超过设置的最大容量'
                ];
            }
    
            if(!$this->checkType()){
                return [
                    'success' => false,
                    'error' => '错误:文件格式不支持'
                ];
            }
    
            $newName = md5(time().rand(1000,5000));
    
            $url = $this->file_path.$newName.".".$this->file['type'];
            $path = dirname(dirname(__FILE__)).$url;
    
            if (move_uploaded_file($this->file['tmp_name'], $path)) {
                return [
                    'success' => true,
                    'url' => $url,
                ];
            }else{
                return [
                    'success' => false,
                    'error' => '错误:上传文件时发生未知错误'
                ];
            }
        }
    }

Comments are closed.
  1. 强势6一波,d=====( ̄▽ ̄*)b