note 102505 added to function.scandir

[email protected] Fri, 18 Feb 2011 01:01:08 -0800
Newsgroups php.notes
Message-ID <[email protected]>
Everyone seems to enjoy using recursive method, while i think recursive method is very inefficient.

The function below will demonstrate how to iteratively (faster than recursive method) dump every object (files and folders) and it's child tree starting from specified path ($dir) :

<?php
function dir_tree($dir) {
   $path = '';
   $stack[] = $dir;
   while ($stack) {
	   $thisdir = array_pop($stack);
	   if ($dircont = scandir($thisdir)) {
		   $i=0;
		   while (isset($dircont[$i])) {
			   if ($dircont[$i] !== '.' && $dircont[$i] !== '..') {
				   $current_file = "{$thisdir}/{$dircont[$i]}";
				   if (is_file($current_file)) {
					   $path[] = "{$thisdir}/{$dircont[$i]}";
				   } elseif (is_dir($current_file)) {
						$path[] = "{$thisdir}/{$dircont[$i]}";
					   $stack[] = $current_file;
				   }
			   }
			   $i++;
		   }
	   }
   }
   return $path;
}

?>
----
Server IP: 69.147.83.197
Probable Submitter: 125.167.30.121
----
Manual Page -- http://www.php.net/manual/en/function.scandir.php
Edit        -- https://master.php.net/note/edit/102505
Del: integrated  -- https://master.php.net/note/delete/102505/integrated
Del: useless     -- https://master.php.net/note/delete/102505/useless
Del: bad code    -- https://master.php.net/note/delete/102505/bad+code
Del: spam        -- https://master.php.net/note/delete/102505/spam
Del: non-english -- https://master.php.net/note/delete/102505/non-english
Del: in docs     -- https://master.php.net/note/delete/102505/in+docs
Del: other reasons-- https://master.php.net/note/delete/102505
Reject      -- https://master.php.net/note/reject/102505
Search      -- https://master.php.net/manage/user-notes.php