note 83126 deleted from function.rmdir by aidan

From: Date: Wed, 15 Apr 2009 12:14:04 +0000
Subject: note 83126 deleted from function.rmdir by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153287@lists.php.net to get a copy of this message
Note Submitter: ornthalas at NOSPAM dot gmail dot com ---- I don't know why, but the glob() function seems to take some time to release file handles. The consequence is some annoying permission denied on random files. Using opendir() instead of glob() fixes the problem. <?php function rm_recursive($filepath) { if (is_dir($filepath) && !is_link($filepath)) { if ($dh = opendir($filepath)) { while (($sf = readdir($dh)) !== false) { if ($sf == '.' || $sf == '..') { continue; } if (!rm_recursive($filepath.'/'.$sf)) { throw new Exception($filepath.'/'.$sf.' could not be deleted.'); } } closedir($dh); } return rmdir($filepath); } return unlink($filepath); } ?> Please note that opendir() method is faster than the glob() method.

« previous php.notes (#153287) next »