自动创建文件结构表示的 PHP 脚本

发布于 2024-10-07 04:24:39 字数 803 浏览 3 评论 0原文

可能的重复:
PHP - 遍历文件夹并显示 HTML 内容

使用 PHP,我正在尝试创建一个脚本,该脚本将导航到网站的根目录,然后从那里使用 scandir()glob() (这些是唯一的我学过的目录扫描功能),我将使用递归方法导航根目录中的所有项目,然后在遇到测试为目录的条目时重新调用自身,通过 is_dir($fileName )

这就是我遇到问题的地方 - 当我到达一个目录条目时,它正确地将 if 语句导航到目录命令,但是当调用自身时,我似乎无法获取 glob() 目录正确,因为每次我调用它时,页面都会停止加载更多内容。我试图从扫描目录的基于相对 URL 的性质中找出我将如何引用它。我设置了一个变量 $ROOT_DIR,它是相对于 php 页面所在目录的根目录(在本例中,$ROOT_DIR="../../"),然后我会从逻辑上思考,我会使用 $ROOT_DIR 调用 scanAllFiles [我的站点地图方法]。 $fileName,这是从字符串中删除前导 "../../" 后找到的目录字符串。尝试了这个之后,还是不行。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经暖 2024-10-14 04:24:39

大多数人只是使用 MySQL 手动制作站点地图。

公开文件并不安全,但您可以添加一些安全措施。

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

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
    }
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文