TP5后台文件上传

// 文件上传
    public function upload(){

        $file =request()->file('file');

        $arr = $file->getInfo(); // 文件名称
        $config = array('size'=>1024*1024*10,'ext'=>array('mp3','mp4','jpg','jpeg','png','gif','avi','wav','rmvb'));
        $info = $file->validate($config)
                     ->move(ROOT_PATH . 'public' . DS . 'upload' . DS . 'music');
        $fileUrl = 'http://'.$_SERVER['HTTP_HOST'].'/demo/public/upload/music/'.$info->getSaveName(); // 文件绝对路径
        if($info){
            $result = Db::name('portal_post')
                    ->data([
                            'more'=>$fileUrl,
                            'create_time' =>time(),
                            'post_status' =>1,
                            'post_type' =>3,
                            'user_id' =>1
                          ])
                    ->insert();
            if ($result) {
                // 成功上传后 获取上传信息
                echo json_encode(array('success'=>$fileUrl,'saveName'=>$arr['name']));
                exit;
            }else{
                echo json_encode(array('flag'=>'上传失败'));
                exit;
            }

        }else{
            // 上传失败获取错误信息
            echo json_encode(array('flag'=>$file->getError()));
            exit;
        }

    }

猜你喜欢

转载自blog.csdn.net/Y_weiguang/article/details/84141518