Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to delete a file from the public folder in Laravel?
In Laravel, you can delete files from the public folder using the File facade or PHP's built-in unlink() function. The File facade provides convenient methods for file operations.
To work with File facade you need to include the class as shown below
use Illuminate\Support\Facades\File;
Using File::delete() Method
The delete() method from the File facade can delete single files or multiple files. It accepts a file path or an array of file paths ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
class UserController extends Controller {
public function index() {
$filePath = public_path('images/img1.jpg');
$deletedFile = File::delete($filePath);
if ($deletedFile) {
echo "File deleted successfully";
} else {
echo "File not found or could not be deleted";
}
}
}
?>
File deleted successfully
Using PHP unlink() Function
The unlink() function is PHP's native method to delete files. It's recommended to check if the file exists before attempting deletion ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller {
public function index() {
$filePath = public_path('images/img3.jpg');
if (file_exists($filePath)) {
$fileDeleted = unlink($filePath);
if ($fileDeleted) {
echo "File deleted successfully";
}
} else {
echo "Unable to delete the given file";
}
}
}
?>
File deleted successfully
Deleting Directories
To delete an entire directory with all its contents, use the deleteDirectory() method from the File facade ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
class UserController extends Controller {
public function index() {
$folderPath = public_path('css');
$folderDeleted = File::deleteDirectory($folderPath);
if ($folderDeleted) {
echo "Directory deleted successfully";
}
}
}
?>
Directory deleted successfully
Deleting Multiple Files
You can delete multiple files by getting all files in a directory and looping through them ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
class UserController extends Controller {
public function index() {
$files = File::allFiles(public_path('testjs'));
foreach ($files as $file) {
$deletedFile = File::delete($file->getPathname());
echo "Deleted: " . $file->getFilename() . "<br>";
}
}
}
?>
Deleted: script2.js Deleted: scripts.js
Comparison
| Method | Use Case | Error Handling |
|---|---|---|
File::delete() |
Single/multiple files | Returns boolean |
unlink() |
Single file only | Throws warning on failure |
File::deleteDirectory() |
Entire directories | Returns boolean |
Conclusion
Use Laravel's File facade for better error handling and Laravel integration. Always check if files exist before deletion and handle errors appropriately to prevent application crashes.
