dpnof28482 2019-07-13 22:41
浏览 124
已采纳

如何在Laravel中将用户ID设置为存储路径?

I'm building an Restful API using Laravel 5 and MongoDB. I'm saving avatar image for users. It's working fine but I'm trying to create a Folder for every User. For example: "app/players/images/USERID"

I've tried to do something like this in different ways but I always get Driver [] is not supported.

\Storage::disk('players'.$user->id)->put($image_name, \File::get($image));

UploadImage:

    public function uploadImage(Request $request)
    {

    $token = $request->header('Authorization');
        $jwtAuth = new \JwtAuth();
    $user = $jwtAuth->checkToken($token, true);

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

        $validate = \Validator::make($request->all(), [

            'file0' => 'required|image|mimes:jpg,jpeg,png'

        ]);

        if ( !$image || $validate->fails() ) 
        {

            $data = array(
                'code' => 400,
                'status' => 'error',
                'message' => 'Image uploading error-'
            );

        }
        else
        {

            $image_name = time().$image->getClientOriginalName();
            \Storage::disk('players')->put($image_name, \File::get($image));

            $user_update = User::where('_id', $user->id)->update(['imagen' => $image_name]);

            $data = array(
                'code' => 200,
                'status' => 'success',
                'user' => $user->id,
                'imagen' => $image_name
            );
        }


    return response()->json($data, $data['code']);
    }

filesystems.php:

        'players' => [
            'driver' => 'local',
            'root' => storage_path('app/players/images/'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

I expect the user avatar image saves on User ID folder.

  • 写回答

2条回答 默认 最新

  • douwu8524 2019-07-13 23:19
    关注

    The disk call, tells Laravel which filesystem to use, let's assume you have an user with Id one, with your code it will access the filesystem playeers1.

    What usually is done is to put these files in folder structures for the different users, so instead you could do. This will put your image file, in the folder 1.

    \Storage::disk('players')->put($user->id . '/' . $image_name, \File::get($image));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀