adrianTNT Posted November 1, 2008 Share Posted November 1, 2008 Hello, I have a function that is supposed to loop over a given directory and remove all its sub files AND folders. The problem is... in some situations it crashes the server, I think it does an infinite loop. I know that it does this when I have MAC files in that folder, whenever this happens I always have a sub folder named "__MACOSX" and inside that folder there are files starting with "._", like ._photo.jpg Can someone please tell me how to fix ths function? It probably has something to do with area $child[0] == '.' // remove all contents of the directory function rmdir_r($path) { if (!is_dir($path)) {return false;} $stack = Array($path); while ($dir = array_pop($stack)) { if (@rmdir($dir)) {continue;} $stack[] = $dir; $dh = opendir($dir); while (($child = readdir($dh)) !== false) { if ($child[0] == '.') {continue;} $child = $dir . DIRECTORY_SEPARATOR . $child; if (is_dir($child)) {$stack[] = $child;} else {unlink($child);} } } return true; } rmdir_r('temp2'); Thank you. Link to comment https://forums.phpfreaks.com/topic/130949-function-causes-infinite-loop/ Share on other sites More sharing options...
Stooney Posted November 1, 2008 Share Posted November 1, 2008 You could always do it like this: (taken from another site, cited at bottom) <? $mydir = "/path/to/dir/"; $d = dir($mydir); while($entry = $d->read()) { if ($entry!= "." && $entry!= "..") { unlink($entry); } } $d->close(); rmdir($mydir); ?> http://www.webmasterworld.com/forum88/3217.htm Link to comment https://forums.phpfreaks.com/topic/130949-function-causes-infinite-loop/#findComment-679863 Share on other sites More sharing options...
genericnumber1 Posted November 1, 2008 Share Posted November 1, 2008 dupe, see also: http://www.phpfreaks.com/forums/index.php/topic,223709.0.html Link to comment https://forums.phpfreaks.com/topic/130949-function-causes-infinite-loop/#findComment-679866 Share on other sites More sharing options...
laffin Posted November 1, 2008 Share Posted November 1, 2008 yer problem lies with this line if ($child[0] == '.') {continue;} it shud compare against . .. if ($child == '.' || $child== '..') {continue;} otherwise any file with . as first chatacter will be ignored, and cause it to fail Link to comment https://forums.phpfreaks.com/topic/130949-function-causes-infinite-loop/#findComment-679872 Share on other sites More sharing options...
Barand Posted November 1, 2008 Share Posted November 1, 2008 Dpuble post - locked Link to comment https://forums.phpfreaks.com/topic/130949-function-causes-infinite-loop/#findComment-679885 Share on other sites More sharing options...
Recommended Posts