In this article, we will see how to remove/delete files from public folders. We will give you a demo of how to remove files from the storage folder in laravel 6 and laravel 7 and laravel 8. Also, you can remove or delete files or images from the public folder and storage folder in laravel 6/7/8/9. So, here we will explain how to delete images from the storage folder using Laravel File System and PHP function file_exists() and unlink().
Many times we are required to delete images or files from our database but those files are still saved in our laravel storage function, if you are not taking any action then they will be occupied more space. So, we need to remove it manually or we can remove it via the laravel function or core PHP function.
You can remove files from the storage path like the below example.
public function removeImage()
{
if(\Storage::exists('upload/img.png')){
\Storage::delete('upload/img.png');
}else{
dd('File not found.');
}
}
In this example, we will delete the image from the public path.
public function removeImage()
{
if(\File::exists(public_path('upload/img.png'))){
\File::delete(public_path('upload/img.png'));
}else{
dd('File not found');
}
}
In this example, we will unlink the image or file using the PHP function.
public function removeImage()
{
if(file_exists(public_path('upload/img.png'))){
unlink(public_path('upload/img.png'));
}else{
dd('File not found');
}
}
You might also like:
Hey there! I'm diving into the world of Laravel 10 and Livewire, and I'm excited to share a step-by-step guide o...
Dec-06-2023
Hello All, In this post i will show you how to connect ftp server using php. PHP provide inbuilt functio...
May-12-2021
In tutorial we will see how to validate laravel 8 image upload validation. In laravel 7/8 you can validate image using t...
Dec-15-2021
In this article, we will see what is new in laravel 9 and the features of laravel 9. Laravel 9 is recently released...
Feb-13-2022