- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to delete a file from the public folder in Laravel?
You can make use of the File facade class. To work with File facade you need to include the class as shown below −
use Illuminate\Support\Facades\File;
Here is a list of examples that shows how to delete the files from the public folder.
The files details in the public folder are as follows –
Example 1
To delete files using delete() method from File façade. The delete() method will delete the files given. It can take a single file or an array of files as an input. You can delete a single file from the public folder as shown below −
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\File; class UserController extends Controller{ public function index() { $images = \File::allFiles(public_path('images')); $deletedFile = File::delete(app_path().'/images/'.$images[1]->getFilename()); if ($deletedFile == null) { echo "File deleted"; } } }
Output
The output of the above code is −
File deleted
Inside the public folder, you will see the file img1.jpg is not present.
Example 2
Using unlink() method in php
unlink(): it helps to delete the given file. If there is a symbolic reference to the file name given the same is deleted too.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { public function index() { if (file_exists(public_path('images/img3.jpg'))){ $filedeleted = unlink(public_path('images/img3.jpg')); if ($filedeleted) { echo "File deleted"; } } else { echo 'Unable to delete the given file'; } } }
Output
The output of the above code is −
File deleted
The files inside the public folder are as follows −
Example 3
The following example shows how to delete a folder inside a public folder. The deleteDirectory() method from File Facade is used to delete a folder. The files along with the folder are completely removed by this method.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Support\Facades\File; class UserController extends Controller{ public function index() { $cssfolder = public_path('css'); $folderdetails = File::deleteDirectory($cssfolder); print_r($folderdetails); } }
Output
The output of the above code is −
1
The public_folder is as follows −
Example 4
Another example is that helps to delete multiple files from a given folder. To test let us add a folder inside the public folder as shown below −
A folder testjs/ is added inside the public folder. Let us delete all the files present in it.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\File; class UserController extends Controller { public function index() { $files = File::allFiles(public_path('testjs')); print_r($files); foreach ($files as $file) { $deletedFile = File::delete(public_path('testjs/'.$file->getFilename())); } } }
Output
The output of the above code is
Array( [0] => Symfony\Component\Finder\SplFileInfo Object( [relativePath:Symfony\Component\Finder\SplFileInfo:private] => [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => script2.js [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\testjs\script2.js [fileName:SplFileInfo:private] => script2.js ) [1] => Symfony\Component\Finder\SplFileInfo Object( [relativePath:Symfony\Component\Finder\SplFileInfo:private] => [relativePathname:Symfony\Component\Finder\SplFileInfo:private] => scripts.js [pathName:SplFileInfo:private] => C:\xampp\htdocs\laraveltest\public\testjs\scripts.js [fileName:SplFileInfo:private] => scripts.js ) )
- Related Articles
- How to access images inside a public folder in Laravel?
- How to upload files in Laravel directly into the public folder?
- How to obtain a list of all files in a public folder in Laravel?
- How to access the public directory in Laravel?
- How can I copy a file from one folder to another folder within a container in Docker?
- How to delete a single record in Laravel 5?
- How to delete or clear caching in Laravel?
- How to move a file from one folder to another using Python?
- How to Delete Specific Line from a Text File in Python?
- How to change variables in the .env file dynamically in Laravel?
- How to delete folder and sub folders using Java?
- How to find all the distinct file extensions in a folder hierarchy (Linux)?
- How to delete a temporary file in Java?
- How to delete the specific node from the XML file using PowerShell?
- Java program to delete certain text from a file
