note 38878 deleted from class.dir by nlopess

From: Date: Sat, 10 Jan 2004 09:55:42 +0000
Subject: note 38878 deleted from class.dir by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-63244@lists.php.net to get a copy of this message
Note Submitter: loicREMOVE AT THATxhtml DOT net ---- Interesting side effect with PHP 4.2.0 (Win32) and the dir class. Supposing you have a folder c:/temp/toto/ you want to remove if empty, first you go through to see if nothing is in it, then you delete it. Try: <?php $dir = 'c:/temp/toto/'; $can_delete = true; $D = dir($dir); while (false !== ($entry = $D->read())) { if ($entry != '.' && $entry != '..') { $can_delete = false; break; } } $D->close(); if ($can_delete) rmdir($dir); ?> You will always have the error: Warning: rmdir() failed (Permission denied) But if you add a closedir() before rmdir() and keep the close() it works! <?php $dir = 'c:/temp/toto/'; $can_delete = true; $D = dir($dir); while (false !== ($entry = $D->read())) { if ($entry != '.' && $entry != '..') { $can_delete = false; break; } } $D->close(); closedir($D->handle); if ($can_delete) rmdir($dir); ?> Maybe solved in new release of PHP. I haven't tested it.

« previous php.notes (#63244) next »