4

Possible Duplicate:
PHP - Iterate through folders and display HTML contents

Using PHP, I'm trying to create a script that will navigate to the root directory of a website, and from there, using scandir() or glob() (those being the only directory scanning functions I've learned), I would use a recursive method to navigate through all the items in the root directory, then re-calling itself when encountering an entry that tested to be a directory, through is_dir($fileName).

Here's where I'm encountering problems - when I reach an entry that is a directory, it correctly navigates the if statement correctly to the commands for directories, but when calling itself, I don't seem to be able to get the glob() directory right, since every time I call it, the page ceases to load anything more. I'm trying to figure out, from the relative URL-based nature of scanning directories how I would reference it. I set a variable $ROOT_DIR, which is the root directory relative to the directory in which the php page is located in (in this case, $ROOT_DIR="../../"), and then I'd think logically, I'd call scanAllFiles [my sitemap method] with $ROOT_DIR . $fileName, where that's the string of the directory found, after removing the leading "../../" from the string. After trying this, it doesn't work.

Should I be using a different directory-traversing method to do this, or am I formatting the method call incorrectly?

Community
  • 1
  • 1
Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
  • How will you distinguish site pages from class/config/function pages? – Russell Dias Dec 13 '10 at 07:35
  • I'm completely uninterested in distinguishing them - I'd want them to be included in the final listing. – Nightfirecat Dec 13 '10 at 07:47
  • are you sure you want to crawl the filesystem instead of the site? you might be exposing pages which are meant to be hidden, including admin pages which should not be in a sitemap. – bcosca Dec 13 '10 at 08:27
  • A code listing would have dramatically increased the value of this question - and probably the answers. – zanlok Dec 13 '10 at 18:07

1 Answers1

2

Most people just use MySQL to make sitemaps, doing it manually.

Exposing files isn't safe, but you can add some security.

<?php
    function files($dir=".") {
        $blacklist = array(str_replace("/","",$_SERVER['SCRIPT_NAME']), 'admin.php', 'users.txt', 'secret.txt');
        $return = array();
        $glob1 = glob($dir."/*");
        for($i=0;$i<=count($glob1)-1;$i++) {
            $item = $glob1[$i];
            $nodir = str_replace($dir, "", $item);
            if(is_dir($item)) {
                $file1 = explode('/', $item);
                $file = $file1[count($file1)-1];
                $merge = array_merge($return, files($item));
                if(!in_array($file, $blacklist) and !empty($nodir)) $return = $merge;
            }
            else {
                $file1 = explode('/', $item);
                $file = $file1[count($file1)-1];
                if(!in_array($file, $blacklist) and !empty($nodir)) $return[] = str_replace("./","",$item);
            }
        }
        return $return;
    }
    // Use like this:
    $files = files(); // Get all files from top folder down, no traling slash  ...
    for($i=0;$i<=count($files)-1;$i++) { // ... Go through them ...
        echo "<li>$files[$i]</li>"; // ... And echo the item
    }
?>
Tanner Ottinger
  • 2,970
  • 4
  • 22
  • 28