How To Delete File From Public Folder In Laravel

WebSolutionStuff | Sep-14-2020 | Categories : Laravel

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 comman functionalites 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');
    }
}

 

Featured Post



Follow us
facebooklogo github instagram twitter