Laravel-Delete files from public/images folder in laravel 5

Sovary May 9, 2022 390
45 seconds read

If you want to delete an image from your server, you have to reference the location of file in directory server, which means you could not reference by URL link to delete it.

Commonly, Laravel 5 file is located in public folder.

For example: your files are located in public/images

$image_path = "/images/filename.ext";  // Value is not URL but directory file path
if(File::exists($image_path)) {
    File::delete($image_path);
}

The example above works only a single file. For deleted multiple files you can use by following overload parameter:

File::delete($file1, $file2, $file3);
//Or put in array
$files = array($file1, $file2);
File::delete($files);


For able to use class File we have to insert a statement on the top of the Controller file you have to import that class by following:

use Illuminate\Support\Facades\File;

This was test on Laravel 5 and working properly.

Reference: here

Laravel 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him