<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/test', [App\Http\Controllers\DemoController::class, 'removeImage'])->name('test');
Route::get('/test2', [App\Http\Controllers\DemoController::class, 'removeImageS'])->name('test2');
<?php
namespace App\Http\Controllers;
use File;
use Illuminate\Http\Request;
use Storage;
class DemoController extends Controller {
/**
* Write code on Construct
*
* @return \Illuminate\Http\Response
*/
public function removeImage(Request $request) {
if (File::exists(public_path('upload/test.png'))) {
File::delete(public_path('upload/test.png'));
} else {
dd('File does not exists.');
}
}
/**
* Write code on Construct
*
* @return \Illuminate\Http\Response
*/
public function removeImageS(Request $request) {
if (Storage::exists('upload/test.png')) {
Storage::delete('upload/test.png');
/*
Delete Multiple File like this way
Storage::delete(['upload/test.png', 'upload/test2.png']);
*/
} else {
dd('File does not exists.');
}
}
}
How to Delete File from Public Folder / Storage Folder in Laravel?
By Hardik Savani May 25, 2020 Category : LaravelPlayUnmuteLoaded: 1.20%Fullscreen
Today, laravel delete file from storage folder is our main topic. In this article, we will implement a how to remove file from public folder in laravel. We will look at example of laravel delete file from public folder. this example will help you laravel remove file from public storage folder.
Sometime, we need to remove file from folder in laravel 6, laravel 7, laravel 8 and laravel 9 application. laravel store file in public folder and storage folder, so most of the cases you simply need to delete file from public folder or storage folder. here we will use File and Storage facade to removing files from folder in laravel application.
You can easily delete file from folder in laravel 5, laravel 6 and laravel 7 in this post solution. so let's see bellow example that will help to remove file from folder. first we will check file is exist or not then we remove it.
Example 1: Laravel Remove File from Public Folder using File Facade
Syntax:
File::delete(file_path);
Example:
In this example, i have one folder "upload" with test.png image in public folder. we will check first file is exist or not then we will remove it.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request;use File; class DemoController extends Controller{ /** * Write code on Construct * * @return \Illuminate\Http\Response */ public function removeImage(Request $request) { if(File::exists(public_path('upload/test.png'))){ File::delete(public_path('upload/test.png')); }else{ dd('File does not exists.'); } }}
Example 2: Laravel Remove File from Storage Folder using Storage Facade
Syntax:
Storage::delete(file_path);
Example:
In this example, i have one folder "upload" with test.png image in public folder. we will check first file is exist or not then we will remove it.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Storage;
class DemoController extends Controller
{
/**
* Write code on Construct
*
* @return \Illuminate\Http\Response
*/
public function deleteImage(Request $request)
{
if(Storage::exists('upload/test.png')){
Storage::delete('upload/test.png');
/*
Delete Multiple File like this way
Storage::delete(['upload/test.png', 'upload/test2.png']);
*/
}else{
dd('File does not exists.');
}
}
}