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
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.
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)
In your case, you can use the following code:
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 thelaravelApp/storage/app/public
directory.With the symbolic link, Laravel looks
laravelApp/storage/app/public/photos/user1.jpeg
path for the request made tohttps://website.com/storage/photos/user1.jpeg
.You can get more information about the symbolic link from the following links: