note 9631 added to readdir

From: Date: Wed, 15 Nov 2000 23:40:09 +0000
Subject: note 9631 added to readdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-7745@lists.php.net to get a copy of this message
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. http://www.php.net/manual/function.readdir.php

« previous php.notes (#7745) next »