{"id":121,"date":"2013-04-04T17:52:40","date_gmt":"2013-04-04T17:52:40","guid":{"rendered":"http:\/\/ecodebook.wordpress.com\/?p=121"},"modified":"2013-04-04T17:52:40","modified_gmt":"2013-04-04T17:52:40","slug":"php-recursive-directory-navigation","status":"publish","type":"post","link":"https:\/\/blog.mycarts.shop\/?p=121","title":{"rendered":"PHP: Recursive Directory Navigation"},"content":{"rendered":"<p>[code language=&#8221;php&#8221;]<br \/>\nfunction readpath($dir,$level,$last,&amp;$dirs,&amp;$files){<br \/>\n\t\/\/print $dir.&quot; (DIR)\\n&quot;;<br \/>\n\t$dp=opendir($dir);<br \/>\n\twhile (false!=($file=readdir($dp)) &amp;&amp; $level == $last){<br \/>\n\t\tif ($file!=&quot;.&quot; &amp;&amp; $file!=&quot;..&quot;)<br \/>\n                {<br \/>\n\t\t\tif (is_dir($dir.&quot;\/&quot;.$file))<br \/>\n\t\t\t{<br \/>\n\t\t\t\treadpath($dir.&quot;\/&quot;.$file,$level+1,$last,$dirs,$files); \/\/ uses recursion<br \/>\n\t\t\t\t$dirs[] = &quot;$dir\/$file&quot;;  \/\/ reads the dir into an array<br \/>\n\t\t\t}<br \/>\n\t\t\telse{<br \/>\n\t\t\t\t$files[] = &quot;$dir\/$file&quot;; \/\/ reads the file into an array<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n\/* From PHP.NET *\/<br \/>\nfunction get_size($path)<br \/>\n   {<br \/>\n       if(!is_dir($path))return filesize($path);<br \/>\n       $dir = opendir($path);<br \/>\n       while($file = readdir($dir))<br \/>\n       {<br \/>\n           if(is_file($path.&quot;\/&quot;.$file))$size+=filesize($path.&quot;\/&quot;.$file);<br \/>\n           if(is_dir($path.&quot;\/&quot;.$file) &amp;&amp; $file!=&quot;.&quot; &amp;&amp; $file !=&quot;..&quot;)$size +=get_size($path.&quot;\/&quot;.$file);<\/p>\n<p>       }<br \/>\n       return $size;<br \/>\n}<\/p>\n<p>if(isset($_GET[&#8216;i&#8217;])){<br \/>\n$start_dir = $_GET[&#8216;i&#8217;]; \/\/ this fetches the i or directory name through a link specified via the url.<br \/>\n}<br \/>\nelse{    \/\/ else if no name is specified by link, then use the default<br \/>\n$start_dir = &quot;.\/parent&quot;;   \/\/ . means the current directory or whatever name you specify<br \/>\n}<\/p>\n<p>$level=1;  \/\/ level is the first level started at<br \/>\n$last=1; \/\/this is set the same as level so the script does not read all directories, and only one at a time<br \/>\n$dirs = array();  \/\/ SET dirs as an ARRAY so it can be read<br \/>\n$files = array(); \/\/SET files as an ARRAY so it can be read<\/p>\n<p>readpath($start_dir,$level, $last, $dirs,$files);<br \/>\n?&gt;<br \/>\n&lt;strong&gt;Sub Directories in: &lt;?php echo substr($start_dir, 2); ?&gt;&lt;\/strong&gt;&lt;br&gt;<br \/>\nsort($dirs);<br \/>\nif(empty($dirs))   \/\/ checks if the dirs array is empty. if so, then display &quot;empty&quot;<br \/>\n{<br \/>\necho&quot;&quot;;<br \/>\n}<br \/>\n\/* SHOWS THE DIRECTORIES FROM ARRAY *\/<br \/>\nforeach($dirs as $dir)<br \/>\n{<br \/>\n\t\techo &quot;&lt;a href=\\&quot;$PHP_SELF?i=$dir\\&quot;&gt;$d&lt;\/a&lt;br&gt; &quot;;<br \/>\n\t}<br \/>\n}<br \/>\n?&gt;<\/p>\n<p>&lt;?php<br \/>\n\/* SHOWS FILES FROM ARRAY*\/<br \/>\n$tf = count($files);<br \/>\nif($tf != 0){<br \/>\n\techo &quot;&lt;br&gt;Total Files: $tf&lt;br&gt;&lt;br&gt;&quot;; \/* Display total in directory*\/<br \/>\n}<br \/>\nsort($files);   \/\/ Sort the files alphabetically<br \/>\n$i = 1;<br \/>\nforeach($files as $file)<br \/>\n{     \/\/Below PHP functions are used to display file stats such as creation time, permissions etc.<br \/>\n$f = preg_replace(&#8216;\/^.*\\\/\\s*\/&#8217;, &#8221;, $file);<br \/>\necho $i;<br \/>\necho &quot;. &lt;a href=\\&quot;$file\\&quot;&gt;$f&lt;\/a&gt;&quot;;<br \/>\necho &quot; size:&quot;.get_size($file).&quot;&lt;br&gt;&quot;;<br \/>\n}<br \/>\n[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[code language=&#8221;php&#8221;] function readpath($dir,$level,$last,&amp;$dirs,&amp;$files){ \/\/print $dir.&quot; (DIR)\\n&quot;; $dp=opendir($dir); while (false!=($file=readdir($dp)) &amp;&amp; $level == $last){ if ($file!=&quot;.&quot; &amp;&amp; $file!=&quot;..&quot;) { if (is_dir($dir.&quot;\/&quot;.$file)) { readpath($dir.&quot;\/&quot;.$file,$level+1,$last,$dirs,$files); \/\/ uses recursion $dirs[] = &quot;$dir\/$file&quot;; \/\/ reads the dir into an array } else{ $files[] = &quot;$dir\/$file&quot;; \/\/ reads the file into an array } } } } \/* From PHP.NET&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=121"}],"version-history":[{"count":0,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/posts\/121\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}