PHP script to automatically create file structure representation [duplicate]

痴心易碎 提交于 2019-12-23 13:32:55

问题


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?


回答1:


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
    }
?>


来源:https://stackoverflow.com/questions/4426629/php-script-to-automatically-create-file-structure-representation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!