note 70714 deleted from function.rmdir by bjori

From: Date: Fri, 27 Oct 2006 10:20:52 +0000
Subject: note 70714 deleted from function.rmdir by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-119170@lists.php.net to get a copy of this message
Note Submitter: graced at monroe dot wednet dot edu ---- The previous example won't work if the directory contains 'hidden' files (those with a leading dot e.g. .htaccess) -- glob() does not catch them. Also, it will follow symlinks and as a result probably delete more than anticipated The below function does not have either of those problems, and is functionally equivalent to "rm -rf" with the exception that it must be called on a directory (changing that is an excercise to a reader). Trailing slashes are allowed but not required -- PHP doesn't care if you're talking about /dir or /dir/ or /dir////. <?php function rmdir_recursive($dir) { $dp = opendir($dir); while(($fn = readdir($dp)) !== false) { if($fn == '.' || $fn == '..') continue; $fn = "{$dir}/{$fn}"; echo "{$fn}\n"; // optional. if(is_dir($fn) && !is_link($fn)) rmdir_recursive($fn); else unlink($fn); } closedir($dp); rmdir($dir); }

« previous php.notes (#119170) next »