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
- /* Is Empty Folder
- ------------------------------------------------------*/
function is_empty_folder($directory=""){
return (count(scandir($directory))
<= 2);
}
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.