Create a Function to Check if a Folder is Empty or Not

Create a Function to Check if a Folder is Empty or Not

At one time or another, as a PHP programmer, you've no doubt needed to check if a directory or folder was empty or not. This PHP tutorial will show you how to accomplish this with a single-line function.

Check if Folder is Empty

Is Empty Folder Function
  1. /* Is Empty Folder
  2. ------------------------------------------------------*/
  3. function is_empty_folder($directory=""){
  4.  
  5. return (count(scandir($directory))<= 2);
  6.  
  7. }

By running scandir, we retrieve all of the directory's contents. We then simply check if there is more than two items (. and .. are just path indexes and aren't actual contents), and that's all! Sleek, simple, and just one line of code needed.

Tags

Add Comment

Leave a Comment or Reply
Fuel the conversation with your thoughts using the form below. Email is not made public.
× Processing...

Please Wait while your Comment is being Processed.

*
*
*

Comments 1 Total Responses