skip to Main Content

I want to remove old images in my public folder when he want to change him profile photo.

ProfileController:

if($request->hasFile('image')){
    $request->validate([
        'image' => 'image|mimes:jpeg,png,jpg,svg|max:2048'
    ]);
            
    $imageName = $request->user()->id.'-'.time().'.'.$request->image->extension();
    $request->image->move(public_path('users'), $imageName);
    $path = "users/".$imageName;
    $request->user()->image = $path;
    $request->user()->save();
}

i tried somethings but i cant do it.
Thanks for your replys.

3

Answers


  1. Chosen as BEST ANSWER

    Thank you for your answer user:8009914(DadoH)

    He writed storage_path(/...) and it doesnt work for me but when i changed to public_path(/..) its now work for me.

    '''
    thats my code example its working:
    $fileToDelete2 = public_path('/').$request->user()->image;
        
        
        
                    if ($request->user()->image && file_exists($fileToDelete2))
                    {
            
                        //this works
                        unlink($fileToDelete2);
        
                    }'''
    

  2. I am allowing to upload avatar for users too, but I am not deleleting old files. But I tried to amend it just now and I found one solution working.

    I was looking also at offical docs, but their solution is not working: https://laravel.com/docs/9.x/filesystem#deleting-files

    Note: I have left few comments and unused variables in the code for better explanation, feel free to do a cleanup.

    UserController.php -> update(Request $request, User $user)

    use IlluminateSupportFacadesStorage;
    ...
    $formFields = $request->validate([
        'avatar' => 'nullable|max:1024|image',
    ]);
    
    if ($request->hasFile('avatar'))
    {
        //this file is impossible to delete, giving up
        $fileToDelete1 = asset($user->avatar);
    
        //this file can be deleted, file_exists returns true
        $fileToDelete2 = storage_path('app/public').'/'.$user->avatar;
        $fileToDelete3 = public_path('/').'/'.$user->avatar;
    
        if ($user->avatar && file_exists($fileToDelete2))
        {
            //not able to delete file like this
            Storage::delete($fileToDelete2);
    
            //this works
            unlink($fileToDelete2);
    
            //or this, depends where you are storing file
            //dont forget to add it to if/file_exists condition
            unlink($fileToDelete3);
    
        }
        $formFields['avatar'] = $request->file('avatar')->store('avatars', 'public');
    }
    $user->update($formFields);
    
    Login or Signup to reply.
  3. In your case, you can use the following code:

        // ...
        $request->validate([
                'image' => 'image|mimes:jpeg,png,jpg,svg|max:2048'
            ]);
    
        // If the user has an image, delete it
        if ($request->user()->image) {
            Storage::delete(public_path($request->user()->image));
        }
    
        // ...
    

    However, uploading files directly to the public folder isn’t a good practice. Most of the experienced Laravel developers prefer to create a symbolic link from the laravelApp/public/storage to the laravelApp/storage/app/public directory.

    With the symbolic link, Laravel looks laravelApp/storage/app/public/photos/user1.jpeg path for the request made to https://website.com/storage/photos/user1.jpeg.

    You can get more information about the symbolic link from the following links:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search