0

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');
});
5
  • add your code for storing file Commented Oct 17, 2018 at 12:53
  • I use $file->storeAs() @SaurabhMistry, It works properly I find it in public folder and in it url Commented Oct 17, 2018 at 12:56
  • Actually in this path "public/upload/users/id" Commented Oct 17, 2018 at 13:11
  • Have you tried changing the file permissions and/or ownership of the file in question? Commented Oct 17, 2018 at 13:33
  • No, I didn't @Goose Commented Oct 17, 2018 at 13:38

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I know it's old, but for me in Windows it somehow didn't work (using Storage::exists($path)) and I'm using WAMPSERVER, check for the permissions, it's fine. idk what make it error.
1

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";
}

3 Comments

Thank u for your response buddy, I test it, it didn't work :(
try to dd($imagepath) , show me what you are getting
I think you are not getting correct path , otherwise unlink will definetly work
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.