laravellaravel-5laravel-5.4php-7.0

Laravel 5.4 - File Storage returns false when checking for file existence


I am trying to see if a file exist in the storage folder. It returns false in the server even if the file exist. In my local environment, it is working perfect.

I am trying to store avatar for users. When I check if avatar exists, it returns false.

Store avatar:

    $avatar = $request->file('avatar');
    $user   = Auth::user();

    if($avatar && $file = $avatar->isValid()) {
        $file = $request->file('avatar');
        $path = $user->avatar_path;
        $image = Image::make($file)->fit(100,100)->encode('jpg');
        Storage::put($path, $image);

        $url = Storage::url($path);
    }

Checking for avatar existence

    $user = Auth::user();
    if ($user->has_avatar) {
        Storage::delete($user->avatar_path);
    }

has avatar method in user.php model

public function getHasAvatarAttribute()
{
    return Storage::disk('public')->has('avatars/'.md5($this->id . $this->email).'.jpg');
}

Avatar path function

public function getAvatarPathAttribute()
{
    return 'public/avatars/'. md5($this->id . $this->email) . '.jpg';
}

When I test on my machine, all is good. On server, it returns false even if avatar exists.


Solution

  • I can not comment Yet so I will leave this answer here. In production it is not working? As in a live server? Maybe linux server? And you are running the local development on Windows? See where I am going with this? Your path maybe wrong. I mean the naming. Even a to A or vice versa may effect the path. The same goes for your file naming. And path naming. avatar/something and /avatar/something are completely different.

    Please let me know if that was the case.