jmurphy1267's avatar

Uplading an image to server

I am trying to upload an image to my server so I can upload it to s3 using intervention. I get this error

    Can't write image data to path (/var/www/yt/storage/avatars/0e93b20e135528a724159680453745fb.jpg)

    public function avatar(Request $request)
    {

        $imageData = $request->get('image');
        $img = Image::make($request->get('image'))->fit(300)->encode('jpg');

        // calculate md5 hash of encoded image
        $hash = md5($img->__toString());

        // use hash as a name
        $path = "avatars/{$hash}.jpg";

        // save it locally 
        $img->save(storage_path($path));

        // $url = "/avatars/{$hash}.jpg"
        $url_local = "/" . $path;


        $path = Storage::putFile('avatars', new File(public_path($url_local)));
        $user = User::find(Auth::id());
        $user->avatar_url = $path;
        if ($user->save()) {
            return response()->json(['avatar' => $user->gen_avatar()]);
        }
    }

Not sure how to fix the issue.

0 likes
1 reply
Yorki's avatar

Does directory /var/www/yt/storage/avatars exists and is writtable?

Please or to participate in this conversation.