How To Delete File From Public Folder In Laravel

Websolutionstuff | Sep-14-2020 | Categories : Laravel PHP

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.

How To Delete File From Public Folder In Laravel 6/7/8/9

Using Storage System

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

 

Using File System

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

 

Using PHP

 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:

Recommended Post
Featured Post
Paypal Payment Gateway Integration In Laravel 8
Paypal Payment Gateway Integra...

In this tutorial, we will see paypal payment gateway integration in laravel 8. Paypal is an international payment p...

Read More

Jan-10-2022

Laravel 9 Form Validation Example
Laravel 9 Form Validation Exam...

 In this tutorial, we will see laravel 9 form validation example. For any incoming data, we need to validate i...

Read More

Feb-12-2022

How To Convert Laravel Query To SQL Query
How To Convert Laravel Query T...

In this article, we will see how to convert a laravel query to an SQL query. Many times we required laravel query builde...

Read More

Oct-27-2022

Laravel 9 Image Upload In Summernote Editor
Laravel 9 Image Upload In Summ...

In this article, we will see laravel 9 image upload in summernote editor. there are many editor available in l...

Read More

Mar-24-2022