note 83307 deleted from function.is-dir by googleguy

From: Date: Tue, 22 Oct 2013 11:00:13 +0000
Subject: note 83307 deleted from function.is-dir by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-197001@lists.php.net to get a copy of this message
Note Submitter: Davy Defaud ---- Well, "John Doe's function" doesn't return the good value when $file != '.' && $file != '..' it must return false and not true ! Moreover, as the latest contributor told us, the directory have to be closed before returning. This one is far safer ;-) : <?php function is_empty_dir($dir) { if ($dh = @opendir($dir)) { while ($file = readdir($dh)) { if ($file != '.' && $file != '..') { closedir($dh); return false; } } closedir($dh); return true; } else return false; // whatever the reason is : no such dir, not a dir, not readable } ?>

« previous php.notes (#197001) next »