0

After a little help I have this recursive function that does its job well, however I need it to create me a link to files.

At the moment the function can only store $dir/$file.php and I need it to create the full path as it loops.

    function siteMap($dir){         

            $scan = scandir($dir);

            foreach ($scan as $file) {
                if ($file === '.' or $file === '..' or $file === '.DS_Store') continue;

                echo '<a href="../' . $file . '">' . $file . '</a><br>';

                if (is_dir($dir . '/' . $file)) {
                    siteMap($dir . '/' . $file);
                }           
            }
        }


        siteMap('application/view');

as you can see this will loop through all the folders and files in a directory and print it to the screen via a link. i will try and include my file structure.

-root(application/view)
--site[+]
---about.php
---new.php
--product[+]
---view.php
---all.php
---search.php

I want to basically create a dynamic site map so every time a new dir or file added it will be included in the site map, this needs to print the parent dir and the content file as a link.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
andrew hutchings
  • 343
  • 1
  • 5
  • 18
  • You know that php already has a implemented way to loop through directories? The RecursiveDirectoryIterator loops through your filesystem and offers you methods helping you to create links. http://www.php.net/manual/de/class.recursivedirectoryiterator.php – SenseException Feb 04 '14 at 21:59
  • yeah i saw that but wanted to practice making recursive functions :D got it to wokr now i missed the $dir varible in the string then just used string replace to tidy the src p.s cant put awnser in for 8 hours lol – andrew hutchings Feb 04 '14 at 23:02

2 Answers2

0

You can do some customization as per your need. look at https://stackoverflow.com/questions/22986093/cannot-loop-through-directories/22986256#22986256

function readDirs($path){
  $dirHandle = opendir($path);
  while($item = readdir($dirHandle)) {
    $newPath = $path."/".$item;
    if(is_dir($newPath) && $item != '.' && $item != '..') {
       echo "Found Folder $newPath<br>";
       readDirs($newPath);
    }
    else{
      echo '&nbsp;&nbsp;Found File or .-dir '.$item.'<br>';
    }
  }
}

$path =  "/";
echo "$path<br>";

readDirs($path);
Community
  • 1
  • 1
user3470953
  • 11,025
  • 2
  • 17
  • 18
0

PHP has built-in RecursiveDirectoryIterator class which uses for for iterating recursively over filesystem directories. This is fast than recursive function. Below code is working on my Windows system. Just change $path. It will link your all files and list as TREE structure.

<?php

$path = realpath('\\\\local\\tech\\projects\\');

echo "<pre>";

$objects = new RecursiveIteratorIterator($RDI = new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
$i=0;
foreach($objects as $name => $object){


    $abspath = str_replace('\\','/',strtolower($name)) ;    
    if($object->isDir()) {

          echo str_repeat("&nbsp;",$i*2) . "<a href='file:///".$abspath ."/' target='_blank'>" .basename($name) . "</a> \n";
          $i++;     
    }
    else {
          echo str_repeat("&nbsp;",$i*2) . "<a href='file:///".$abspath ."' target='_blank'>" .basename($name) . "</a> \n";
    }
    if($RDI->hasChildren() == false ) {
        $i = 0 ; 
    }
}

?>

shyammakwana.me
  • 5,562
  • 2
  • 29
  • 50