How To Delete File From Public Folder In Laravel

Websolutionstuff
1 min readJan 29, 2021

--

  • In this post we will see how to remove/delete file from public storage, today I will give you demo how to remove file from storage folder in laravel.
  • So, here I will explain how to delete image from storage folder using Laravel File System and php function file_exists() and unlink().
  • Images or file uploads are same functionalities in web developing, many time we required to delete images or file from our database but those files are still saved in our laravel storage function.
  • If you are not take any action then it will be occupies more space So we need to remove it manually or we can remove via laravel function or core PHP function.

Using Storage System

public function removeImage()
{
if(\Storage::exists('upload/img.png')){
\Storage::delete('upload/img.png');
}else{
dd('File not found.');
}
}

Using File System

public function removeImage()
{
if(\File::exists(public_path('upload/img.png'))){
\File::delete(public_path('upload/img.png'));
}else{
dd('File not found');
}
}

Using PHP

public function removeImage()
{
if(file_exists(public_path('upload/img.png'))){
unlink(public_path('upload/img.png'));
}else{
dd('File not found');
}
}

--

--

Websolutionstuff
Websolutionstuff

Written by Websolutionstuff

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API. Visit Website: http://websolutionstuff.com/

No responses yet