note 110089 deleted from function.is-dir by googleguy
| From: | googleguy@php.net | Date: | Tue, 18 Sep 2012 18:21:43 +0000 |
| Subject: | note 110089 deleted from function.is-dir by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190866@lists.php.net to get a copy of this message | ||
Note Submitter: santhosh dot palan at nospam dot com
----
Below is a simple recursive function check whether a directory/folder is empty . Please let me know
of any bugs or improvements that could be possible :
<?php
$path="/home/santhosh/empty_test";
if(empty_dir($path))
{
echo("$path is empty\n");
}
else
{
echo("$path is NOT empty\n");
}
function empty_dir($dirpath)
{
$empty=1;
if (is_dir($dirpath))
{
if ($dh = opendir($dirpath))
{
while (($file = readdir($dh)) !== false)
{
if($file<>'.' && $file<>'..' && $file)
{
if(is_dir("$dirpath/$file"))
{
if(!empty_dir("$dirpath/$file"))
{
$empty=0;
break;
}
}
else
{
$empty=0;
break;
}
}
}
}
}
else
{
$empty=0;
}
return $empty;
}