Issue on delete a file in laravel 5

- 1 answer

Ad

I'm trying to delete a file in laravel 5 application . My controller code is

public function deleteImages(){
        $image = public_path() . '/uploads/images/'.Input::get('image');
        Storage::Delete($image);
    }

i'm getting an error message like

local.ERROR: exception 'League\Flysystem\FileNotFoundException' with message 'File not found at path: var/www/html/wonders/uploads/images/Copy1Copy1Kuruva-Islands.jpg' in /var/www/html/wonders/vendor/league/flysystem/src/Filesystem.php:381

But the file is really existing and the path is correct. But i'm wondering why it shows such an error.

Ad

Answer

Ad

In the documentation I found:

When using the local driver, note that all file operations are relative to the root directory defined in your configuration file. By default, this value is set to the storage/app directory. Therefore, the following method would store a file in storage/app/file.txt:

Storage::disk('local')->put('file.txt', 'Contents');

The way I see it you can't use Storage::delete here because you are trying to delete a file in your public folder.

Try one of these that I found on Laravel Recipies:

// Delete a single file
\File::delete($filename);

// Delete multiple files
\File::delete($file1, $file2, $file3);

// Delete an array of files
$files = array($file1, $file2);
\File::delete($files);

I've added a leading \ because you are using Laravel 5.

Ad
source: stackoverflow.com
Ad