note 9631 deleted from function.readdir by tularis

From: Date: Sun, 13 May 2007 19:08:53 +0000
Subject: note 9631 deleted from function.readdir by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-126568@lists.php.net to get a copy of this message
Note Submitter: CK1 at wwwtech dot de ---- funny thing ;) if you really read your dirs with subdirs into an array like described above, and there's a link like ln -s ./ ./recursive you'll get an endless loop ;) My suggestion to prevent this is to limit the number of recursion instances: Change the function to function GetDirArray($sPath,$maxinst,$aktinst = 0) { //Load Directory Into Array $handle=opendir($sPath); while ($file = readdir($handle)) { $retVal[count($retVal)] = $file; } //Clean up and sort closedir($handle); sort($retVal); while (list($key, $val) = each($retVal)) { if ($val != "." && $val != "..") { $path = str_replace("//","/",$sPath.$val);  if (is_dir($sPath.$val) && ($aktinst < $maxinst || $maxinst == 0)) { GetDirArray($sPath.$val."/",$maxinst,$aktinst+1); } } } } A call could be this: $dir = GetDirArray("./",0); Another method could be a regexp, which looks for more than $maxinst same named subdirs: if (is_dir($sPath.$val) && !preg_match("/\/(\w+)(\/\\1){$max,}/",$sPath.$val)) { GetDirArray($sPath.$val."/",$maxinst); } instead of the if-construction above.

« previous php.notes (#126568) next »