@jsphpndr Why are you manually generating “random” filenames? That’s literally what Laravel does if you use its file uploading helpers proplerly. See: https://laravel.com/docs/11.x/requests#storing-uploaded-files
All you need to do is upload the new file, and set up some sort of listener that deletes the old file from storage if the column is updated on your model:
if ($image = $request->file('image')) {
$client->update([
'image' => $image->store('uploads'),
]);
}
And then to clean up old images:
class ClientObserver
{
public function saved(Client $client)
{
if ($client->isDirty('image')) {
Storage::delete($client->getOriginal('image'));
}
}
}
You also should be using Laravel’s disk properly, instead of all that manual string-replacing paths and folders names and whatnot.
Also, you should not be storing user-uploaded files in a publicly-accessible location, because if a user manages to upload a malicious file they can now access and use that file to hack your web application and the server that it is running on. You should instead be uploading files to storage, and then serving generated thumbnails of those images. You can either pre-generate the thumbnails if you know the dimensions you need and store those in a publicly-accessible directory, or you should use something like Glide to dynamically generate and serve thumbnails.