note 98215 deleted from function.rmdir by danbrown

From: Date: Tue, 01 Jun 2010 17:14:44 +0000
Subject: note 98215 deleted from function.rmdir by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169812@lists.php.net to get a copy of this message
Note Submitter: John Galt ---- Yet another recursive directory deleting function. I made it because it uses less code and chmod()'s files before attempting to delete them, just in case permissions are denied. <?php /*************************************************** * _rmdir(string $path) * * Deletes the contents of and removes a directory * * * * @param string $path Path to the directory * * * * Returns bool * * true if deletion is entirely successful * * false if deletion is not entirely successful * ***************************************************/ function _rmdir($path) { $path = ((substr($path, -1) != PATH_SEPARATOR) ? ($path.PATH_SEPARATOR) : ($path)); if(($open = opendir($path)) === false) return false; while(($fn = readdir($open)) !== false) if($fn != "." && $fn != "..") { @chmod($path.$fn, 0777); if(((is_file($path.$fn) && !unlink($path.$fn)) || (is_dir($path.$fn) && !_rmdir($path.$fn)))) return false; } closedir($open); return rmdir($path); } ?> As you can see, it also checks if the directory exists before deleting, and appends a slash separator to the path if necessary. And it doesn't use glob() because I'm running off an old PHP server that doesn't support it. I don't know which takes more server resources, glob() or opendir()/readdir()/closedir().

« previous php.notes (#169812) next »