jmurphy1267's avatar

File cannot be found

I have this method that uploads a file to the server then sends it to s3.

 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 = "images/{$hash}.jpg";

    // save it locally to ~/public/images/{$hash}.jpg
    $img->save(public_path($path));

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


    $path = Storage::putFile('avatars', new File($url_local));
    $user = User::find(Auth::id());
    $user->avatar_url = $path;
    if ($user->save()) {
        return redirect()->route('user_profile_settings');
    }
}

Once the file is uploaded to the server I am getting this error:

 The file "/images/0e93b20e135528a724159680453745fb.jpg" does not exist
0 likes
1 reply
Cronix's avatar
Cronix
Best Answer
Level 67

Probably because you saved it using pubic_path() here $img->save(public_path($path));

So you'd need to use public_path() to retrieve it as well.

 $url_local = public_path($path);

It's also not a url, it's a file path...

1 like

Please or to participate in this conversation.