{"id":819,"date":"2013-06-17T22:09:22","date_gmt":"2013-06-17T22:09:22","guid":{"rendered":"http:\/\/blog.corbdesign.com\/?p=819"},"modified":"2013-06-17T22:09:22","modified_gmt":"2013-06-17T22:09:22","slug":"recursivephp","status":"publish","type":"post","link":"https:\/\/corbinrose.com\/blog\/technology\/recursivephp\/","title":{"rendered":"Get files in directory recursively [php]"},"content":{"rendered":"\n<p>Getting files from a directory can be useful if you want to access your files but forgot what subfolder names you put there (if you&#8217;ve turned indexing off, you can&#8217;t just look at the dir url and see an autogenerated list). What I do sometimes is make a php file with some wonky name, like &#8220;zebraBreath.php&#8221; and make one myself:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 1<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>\/**\n * @param $path A directory\n * @param array $args Argument array:\n *      hidden: If true, hidden files beginning with a dot will be included\n *      ignoreRef: If true, . and .. are ignored\n *      recursive: If true, this function is recursive\n *      ignore: An array of paths to ignore\n * @return array Files in the directory\n *\/\npublic static function getFiles($path, $args = array()) {\n    $hidden = self::set_default($args[&#39;hidden&#39;], TRUE);\n    $ignoreRef = self::set_default($args[&#39;ignoreRef&#39;], TRUE);\n    $recursive = self::set_default($args[&#39;recursive&#39;], FALSE);\n    $ignore = self::set_default($args[&#39;ignore&#39;], NULL);\n\n    $ignore_map = array();\n    if ($ignore) {\n        foreach ($ignore as $i) {\n            if (is_dir($i)) {\n                $i = CrayonUtil::path_slash($i);\n            }\n            $ignore_map[$i] = TRUE;\n        }\n    }\n\n    $files = glob($path . &#39;*&#39;, GLOB_MARK);\n    if ($hidden) {\n        $files = array_merge($files, glob($path . &#39;.*&#39;, GLOB_MARK));\n    }\n    if ($ignoreRef || $ignore) {\n        $result = array();\n        for ($i = 0; $i &lt; count($files); $i++) {\n            $file = $files[$i];\n            if (!isset($ignore_map[$file]) &amp;&amp; (!$ignoreRef || (basename($file) != &#39;.&#39; &amp;&amp; basename($file) != &#39;..&#39;))) {\n                $result[] = $file;\n                if ($recursive &amp;&amp; is_dir($file)) {\n                    $result = array_merge($result, self::getFiles($file, $args));\n                }\n            }\n        }\n    } else {\n        $result = $files;\n    }\n    return $result;\n}<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Method 2<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>function getDirectory( $path = &#39;.&#39;, $level = 0 ){\n\n   $ignore = array( &#39;cgi-bin&#39;, &#39;.&#39;, &#39;..&#39;);\n   \/\/ Directories to ignore when listing output. Many hosts\n   \/\/ will deny PHP access to the cgi-bin.\n\n   $dh = @opendir( $path );\n   \/\/ Open the directory to the handle $dh\n\n   while( false !== ( $file = readdir( $dh ) ) ){\n   \/\/ Loop through the directory\n\n       if( !in_array( $file, $ignore ) ){\n       \/\/ Check that this file is not to be ignored\n\n           $spaces = str_repeat( &#39;&nbsp;&#39;, ( $level * 4 ) );\n           \/\/ Just to add spacing to the list, to better\n           \/\/ show the directory tree.\n\n           if( is_dir( &quot;$path\/$file&quot; ) ){\n           \/\/ Its a directory, so we need to keep reading down...\n              if($level==0){\n                echo &quot;&lt;strong&gt;$spaces $file&lt;\/strong&gt;&lt;br&gt;&quot;;\n              } else {\n                echo &quot;$spaces $file&lt;br&gt;&quot;;\n              }\n               getDirectory( &quot;$path\/$file&quot;, ($level+1) );\n               \/\/ Re-call this same function but on a new directory.\n               \/\/ this is what makes function recursive.\n\n           } else {\n\n               \/\/echo &quot;$spaces $file&lt;br&gt;&quot;;\n               \/\/ Just print out the filename\n\n           }\n       }\n   }\n   closedir( $dh );\n   \/\/ Close the directory handle\n}<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Method 3<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>function corbDirectory($where) {\n  $myDirectory = opendir($where);\n\n  \/\/ get each entry\n  while($entryName = readdir($myDirectory)) {\n     if ($entryName != &quot;.&quot; &amp;&amp; $entryName != &quot;..&quot;) {\n        $dirArray[] = $entryName;\n     }\n  }\n  \/\/ close directory\n  closedir($myDirectory);\n\n  \/\/   count elements in array\n  $indexCount   = count($dirArray);\n  sort($dirArray);\n \n  for($index=0; $index &lt; $indexCount; $index++){\n     $realname = basename(&quot;$dirArray[$index]&quot;, &quot;.php&quot;);\n     $realname = eregi_replace(&quot;_&quot;, &quot; &quot;, $realname);\n     $realname = ucwords($realname);\n\n     $pos = strpos($dirArray[$index], &quot;.&quot;);\n     if($pos===false) {\n\n        print(&quot;&lt;a href=&quot;directoryStructure.php?chosen={$where}\/{$dirArray[$index]}&quot;&gt;$dirArray[$index]&lt;\/a&gt;n&lt;br&gt;&quot;);\n     } else {\n        print(&quot;&lt;a href=&quot;{$where}\/{$dirArray[$index]}&quot;&gt;$dirArray[$index]&lt;\/a&gt;n&lt;br&gt;&quot;);\n     }\n  }\n  print(&quot;&lt;br&gt;&quot;);\n}<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Method 4<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>function scanIt($where) {\n  $files1 = scandir($where);\n  foreach ($files1 as $value) {\n    echo $value . &quot;&lt;br&gt;&quot;;\n  }\n}<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Getting files from a directory can be useful if you want to access your files but forgot what subfolder names you put there (if you&#8217;ve turned indexing off, you can&#8217;t just look at the dir url and see an autogenerated list). What I do sometimes is make a php file with some wonky name, like [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":5540,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[28,17],"tags":[],"class_list":["post-819","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","category-technology","clearfix","post-index"],"acf":[],"jetpack_featured_media_url":"https:\/\/corbinrose.com\/blog\/wp-content\/uploads\/2013\/06\/pexels-photo-6549629-2.jpeg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/posts\/819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/comments?post=819"}],"version-history":[{"count":0,"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/posts\/819\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/media\/5540"}],"wp:attachment":[{"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/media?parent=819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/categories?post=819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/corbinrose.com\/blog\/wp-json\/wp\/v2\/tags?post=819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}