Q: File exist not working in laravel for a file that right there In my laravel project, I have a form for uploading a file and removing it.
I create a disk in config/filesystem:
'upload' => [
            'driver' => 'local',
            'root' => public_path(),
            'url' => env('APP_URL'),
            'visibility' => 'public',
        ],

Here uploading file work correctly but when I want remove them from public folder the file not exist:
$img_path = public_path( $this->directory . '/' . $this->getFliename());
$img_path = str_replace( "/",'\\', $img_path); // checked with it & without it

// $img_path= "C:\Users\Me\Desktop\final\public\upload\users\1\5bc722d2b7c05.jpg"

dump(File::exists($img_path)); // it always return false

// and then which one
File::delete($img_path); 
Storage::disk('upload')->delete($img_path);

why this file doesn't exist, and which one for deleting a file is true?
It becomes more complicating when I do it with route???
// testing ...
Route::get('/test', function (){
   File::delete('C:\Users\Me\Desktop\final\public\upload\users\1\5bc722d2b7c05.jpg');
});


A: As you are storing images in public/upload folder :
$user_id= $user->id;
$file_name=$user->image;
$img_path=public_path('upload/users/'.$user_id. '/' .$filename);

if(is_file($img_path)){
   unlink($img_path);
   // or File::delete($img_path);  
}else{
    echo "File does not exist";
}


A: The file might exist on the filesystem, but the web user that PHP is running under might not have permission to read, update, or delete the file.
On a linux or mac system, you'll typically want to give the web user ownership of the file, and set file permissions to 664 and folder permissions to 775.
644 and 755 should also work and give you slightly stricter permissions.
If the above fails, try 777 just to debug, but do not do that on production as it's a serious security issue.
I'm not sure how it is done on Windows, but hopefully this gets you on the right path.

A: Your $img_path appears to be set with the full path to the file, rather than a path relative to the base of your specified disk that would have been returned by $file->storeAs() when you saved the file.
This should work:
Storage::disk('upload')->delete('upload/users/1/5bc722d2b7c05.jpg')
If you need the full path on disk for some reason, try this:
Storage::disk('upload')->path('upload/users/1/5bc722d2b7c05.jpg')
Check if the file exists:
Storage::disk('upload)->exists('upload/users/1/5bc722d2b7c05.jpg')
OR File::exists(Storage::disk('upload')->path('upload/users/1/5bc722d2b7c05.jpg'))
(returns C:\Users\Me\Desktop\final\public\upload/users/1/5bc722d2b7c05.jpg because your 'upload' disk has a base of public_path())
Read/write permissions for the web server could also come into play, depending on your server configuration. Typically you would not store things directly in the public/ folder, but rather in storage/app/public and use php artisan storage:link to generate a symbolic link to the storage directory. You can read more here: https://laravel.com/docs/5.7/filesystem#the-public-disk
As a side note, Windows accepts either / or \, so using / makes your scripts much easier to use on other systems and reduces headaches in string escaping.
