上传文件,原生处理,记录一下

private $config = [
        'image' => [
            'validate' => [
                'size' => 10*1024*1024,
                'ext'  => 'jpg,png,gif,jpeg',
            ],
            'rootPath'      =>  './Uploads/images/', //保存根路径
        ],
        'audio' => [
            'validate' => [
                'size' => 100*1024*1024,
                'ext'  => 'mp3,wav,cd,ogg,wma,asf,rm,real,ape,midi',
            ],
            'rootPath'      =>  './Uploads/audios/', //保存根路径
        ],
        'video' => [
            'validate' => [
                'size' => 100*1024*1024,
                'ext'  => 'mp4,avi,rmvb,rm,mpg,mpeg,wmv,mkv,flv',
            ],
            'rootPath'      =>  './Uploads/videos/', //保存根路径
        ],
        'file' => [
            'validate' => [
                'size' => 5*1024*1024,
                'ext'  => 'doc,docx,xls,xlsx,pdf,ppt,txt,rar',
            ],
            'rootPath' =>  './Uploads/files/', //保存根路径
        ],
    ];
    private $domain;
    function __construct()
    {
        //获取当前域名
        $this->domain = Request::instance()->domain();
    }



    public function upload($fileName){
        if(empty($_FILES) || empty($_FILES[$fileName])){
            return '';
        }
        try{
            $file = request()->file($fileName);
            if (is_array($file)){
                $path = [];
                foreach ($file as $item){
                    $path[] =  $this->save($item);
                }
            } else {
                $path = $this->save($file);
            }
            return $path;
        } catch (\Exception $e){
            $arr = [
                'status' => 0,
                'message' => $e->getMessage(),
            ];
            header('Content-Type: application/json; charset=UTF-8');
            exit(json_encode($arr));
        }
    }


    public function uploadDetail($fileName){
        if(empty($_FILES) || empty($_FILES[$fileName])){
            return [];
        }
        try{
            $file = request()->file($fileName);
            if (is_array($file)){
                $path = [];
                foreach ($file as $item){
                    $detail = $item->getInfo();
                    $returnData['name'] = $detail['name'];
                    $returnData['type'] = $detail['type'];
                    $returnData['size'] = $detail['size'];
                    $returnData['filePath'] = $this->save($item);
                    $returnData['fullPath'] = $this->domain.$returnData['filePath'];
                    $path[] = $returnData;
                }
            } else {
                $detail = $file->getInfo();
                $returnData['name'] = $detail['name'];
                $returnData['type'] = $detail['type'];
                $returnData['size'] = $detail['size'];
                $returnData['filePath'] = $this->save($file);
                $returnData['fullPath'] = $this->domain.$returnData['filePath'];
                $path = $returnData;
            }
            return $path;
        } catch (\Exception $e){
            $arr = [
                'status' => 0,
                'message' => $e->getMessage(),
            ];
            header('Content-Type: application/json; charset=UTF-8');
            exit(json_encode($arr));
        }
    }

    private function getConfig($file){
        $name = pathinfo($file['name']);
        $end = $name['extension'];
        foreach ($this->config as $key=>$item){
            if ($item['validate']['ext'] && strpos($item['validate']['ext'], $end) !== false){
                return $this->config[$key];
            }
        }
        return null;
    }
    private function save(&$file){
        $config = $this->getConfig($file->getInfo());
        if (empty($config)){
            throw new Exception('上传文件类型不被允许!');
        }
        // 移动到框架应用根目录/uploads/ 目录下
        if ($config['validate']) {
            $file->validate($config['validate']);
            $result = $file->move($config['rootPath']);
        } else {
            $result = $file->move($config['rootPath']);
        }
        if($result){
            $path = $config['rootPath'];
            if (strstr($path,'.') !== false){
                $path = str_replace('.', '', $path);
            }
            return $path.$result->getSaveName();
        }else{
            // 上传失败获取错误信息
            throw new Exception($file->getError());
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值