How to check File exist in Folder or Not in Laravel 8?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Hello Dev’s,

Now in this article, I will teach you how to check file in folder exists or not in your project. If we need to check if file is exists or not in a given path then we can check it easily. Normally we always need to check file exists in a folder in our project.

Now in this example we can just use File object.

Solution 1 for Laravel

At first we can create a folder then after we can create file in this folder.

public function index()
   {
	  if (File::exists(public_path('img/dummy.jpg'))) {
		dd('File is Exists');
	  }else{
	 	dd('File is Not Exists');
	  }
   }

If it will return File is Exists, file available or file is not exists file is not available.

In this very simple example we can do by use Storage object.

Solution 2 for Laravel

At first we need create a folder then we can create a new file in this folder.

public function index()
   {
	  if (Storage::exists(public_path('img/dummy.jpg'))) {
		dd('File is Exists');
	  }else{
		dd('File is Not Exists');
	  }
   }

If return File is Exists it show file available or file is not exists it show file is not available.

Solution for PHP

Now we will see this on raw php. At first we need to create a folder after that we can a create new file in this folder.

In this method we can use only function is file_exists().

public function index()
   {
	  if (file_exists(public_path('img/dummy.jpg'))) {
		dd('File is Exists ');
	  }else{
	 	dd('File is Not Exists');
	  }
   }

If return File is Exists then file available or file is not exists then file is not available.

Read Also : How to Get all Files in a Directory in Laravel 8 -?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →