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 Qt4代码实现下面的界面
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码