note 72598 deleted from function.rmdir by aidan

From: Date: Wed, 15 Apr 2009 04:24:55 +0000
Subject: note 72598 deleted from function.rmdir by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153257@lists.php.net to get a copy of this message
Note Submitter: eli dot hen at gmail dot com ---- This functions deletes or empties the directory. Without using recursive functions! <?php /** * Removes the directory and all its contents. * * @param string the directory name to remove * @param boolean whether to just empty the given directory, without deleting the given directory. * @return boolean True/False whether the directory was deleted. */ function deleteDirectory($dirname,$only_empty=false) { if (!is_dir($dirname)) return false; $dscan = array(realpath($dirname)); $darr = array(); while (!empty($dscan)) { $dcur = array_pop($dscan); $darr[] = $dcur; if ($d=opendir($dcur)) { while ($f=readdir($d)) { if ($f=='.' || $f=='..') continue; $f=$dcur.'/'.$f; if (is_dir($f)) $dscan[] = $f; else unlink($f); } closedir($d); } } $i_until = ($only_empty)? 1 : 0; for ($i=count($darr)-1; $i>=$i_until; $i--) { echo "\nDeleting '".$darr[$i]."' ... "; if (rmdir($darr[$i])) echo "ok"; else echo "FAIL"; } return (($only_empty)? (count(scandir)<=2) : (!is_dir($dirname))); } ?>

« previous php.notes (#153257) next »