Jump to content

Recommended Posts

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

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

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.