note 96981 deleted from function.rmdir by danbrown

From: Date: Thu, 25 Mar 2010 19:21:48 +0000
Subject: note 96981 deleted from function.rmdir by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-167419@lists.php.net to get a copy of this message
Note Submitter: kennethjor at gmail dot com ---- Addition to bcairns', kevin's, and anonymous' below. 1. Add a directory separator to the dir always, otherwise $dir."*" might match sibling directories. E.g. deleting /var/foo would turn into "/var/foo*" once it got to glob(), meaning /var/foobar would also be history. 2. If it's a file, delete it right off the back before sending it to glob 3. If the directory doesn't exist, return false. 4. Make sure we don't EVER delete root function delTree($dir) { if (is_file($dir)) return unlink($file); if (is_dir($dir) === false) return false; // Root ? if (dirname($dir) == $dir) die("Tried to delete root\n"); $files = glob( $dir . DIRECTORY_SEPARATOR . '*', GLOB_MARK ); foreach( $files as $file ){ if( is_dir( $file ) ) delTree( $file ); else unlink( $file ); } if (is_dir($dir)) rmdir( $dir ); }

« previous php.notes (#167419) next »