{"id":443,"date":"2005-12-22T00:07:35","date_gmt":"2005-12-21T22:07:35","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2005\/12\/22\/php-non-recursive-function-through-directories\/"},"modified":"2007-05-08T16:01:46","modified_gmt":"2007-05-08T14:01:46","slug":"php-non-recursive-function-through-directories","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2005\/12\/php-non-recursive-function-through-directories\/","title":{"rendered":"PHP Non Recursive Function Through Directories"},"content":{"rendered":"<p>I needed a recursive function to go through directories and subdirectories, so I headed to PHP.net&#39;s <a href=\"http:\/\/php.net\/readdir\">readdir()<\/a> page, knowing that I would find one in user comments. Or did I need a recursive function ? Hell no. I&#39;ve found the nifty neat following function, which does not recursively calls itself. And what&#39;s so cool about it, you may ask ? Speed.<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php\r\nfunction list_directory($dir) {\r\n   $file_list = &#039;&#039;;\r\n   $stack[] = $dir;\r\n   while ($stack) {\r\n      $current_dir = array_pop($stack);\r\n      if ($dh = opendir($current_dir)) {\r\n         while (($file = readdir($dh)) !== false) {\r\n            if ($file !== &#039;.&#039; AND $file !== &#039;..&#039;) {\r\n               $current_file = &quot;{$current_dir}\/{$file}&quot;;\r\n               if (is_file($current_file)) {\r\n                  $file_list[] = &quot;{$current_dir}\/{$file}&quot;;\r\n               } elseif (is_dir($current_file)) {\r\n                  $stack[] = $current_file;\r\n               }\r\n            }\r\n         }\r\n      }\r\n   }\r\n   return $file_list;\r\n}\r\n?&gt;<\/code><\/pre><\/div>\n<p>The function returns an array of files from directory passed as argument and subdirectories. The user who submitted this function added a few benchmarks against an average recursive function, to find out that this one is about 50% quicker.<\/p>\n<p>Just as anybody I guess, I&#39;ve been using various recursive functions for years now, and I just had never thought about why or how to do it differently. Damn. So many CPU cycles wasted for nothing :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Best way to go through (sub)directories<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[2,10],"class_list":["post-443","post","type-post","status-publish","format-standard","hentry","tag-code","tag-php"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=443"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/443\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}