PHP脚本自动创建文件结构表示 [英] PHP script to automatically create file structure representation

查看:152
本文介绍了PHP脚本自动创建文件结构表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

PHP - 通过文件夹迭代并显示HTML内容

使用PHP,我正在尝试创建一个脚本,该脚本将导航到网站的根目录,然后使用 scandir() glob()(那些是我学到的唯一的目录扫描函数),我将使用递归方法浏览根目录中的所有项目,然后重新调用通过 is_dir($ fileName)

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).

这里是我在哪里遇到问题 - 当我到达一个目录的条目时,它正确地将if语句正确导航到目录的命令,但是当调用自己时,我似乎无法获得水珠()目录正确,因为每次调用它,该页面不再加载任何东西。我试图找出,从扫描目录的相对URL的性质我将如何引用它。我设置一个变量 $ ROOT_DIR ,它是相对于php页面所在目录的根目录(在这种情况下, $ ROOT_DIR =../../),然后我认为逻辑上,我会用$ ROOT_DIR调用scanAllFiles [我的网站地图方法]。 $ 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.

我应该使用不同的目录遍历方法来执行此操作,还是我错误地格式化方法调用? p>

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

推荐答案

大多数人只是使用MySQL进行站点地图,手动进行。

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

这篇关于PHP脚本自动创建文件结构表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆