PHP script to automatically create a file structure view

Possible duplicate:
PHP - Iterate through folders and display HTML content

Using PHP, I am trying to create a script that goes to the root directory of the website, and from there using scandir() or glob() (those that are the only directory scanning functions I have learned), I would use a recursive method for navigate all the elements of the root directory, and then repeatedly called itself when it came across an entry that was tested as a directory through is_dir($fileName) .

Here, where I run into problems - when I get to an entry that is a directory, it correctly correctly translates the if statement to directory commands, but when I call myself, I seem to be unable to get glob() , since every time I call it, the page stops loading anything else. I am trying to figure out from the relative nature of crawl directories based on URLs how I will link to it. I set the $ROOT_DIR variable, which is the root directory relative to the directory where the php page is located (in this case, $ROOT_DIR="../../" ), and then I would logically assume I would call scanAllFiles [mine sitemap method] with $ ROOT_DIR. $ fileName where the specified line is found after removing the leading line "../../" from the line. Having tried, this will not work.

Should I use a different directory navigation method for this, or am I formatting the method call incorrectly?

+4
source share
1 answer

Most people simply use MySQL to create sitemaps, doing it manually.

Exposing files is unsafe, 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 } ?> 
+2
source

All Articles