Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果index.php存在于每个子目录中,如何签入php_Php_Html - Fatal编程技术网

如果index.php存在于每个子目录中,如何签入php

如果index.php存在于每个子目录中,如何签入php,php,html,Php,Html,有没有办法检查我的子目录中是否存在index.php 例如: home/ |__folder/ | |__index.php | |__style.css | |__folder2/ | |__style.css | |__folder3/ |__text.txt 我想检查文件夹folder2和folder3中是否有index.php。 最后,如果index.php不存在,我应该创建它。编写了一个快速类,从命令提示符/terminal运行 class FolderChecker {

有没有办法检查我的子目录中是否存在index.php

例如:

home/
|__folder/
|  |__index.php
|  |__style.css
|
|__folder2/
|  |__style.css
|
|__folder3/
   |__text.txt
我想检查文件夹folder2和folder3中是否有index.php。
最后,如果index.php不存在,我应该创建它。

编写了一个快速类,从命令提示符/terminal运行

class FolderChecker {

    public function __construct() {
        $this->scan_folder(getcwd());
    }

    public function scan_folder($folder) {
        $scandir = scandir($folder);

        if(file_exists($folder . "/index.php")) {
            echo "index.php file exists in $folder\n";
        } else {
            echo "index.php file DOES NOT exists in $folder\n";
        }

        foreach($scandir as $scan) {
            if($scan == "." || $scan == ".." || is_file($folder . '/' . $scan))
                continue;

            if(is_dir($folder . '/' . $scan)) 
                $this->scan_folder($folder . '/' . $scan);
        }
    }

}

new FolderChecker;

到目前为止你试过什么?看一看文档页面上的is_文件、is_dir、glob和递归glob函数示例:我相信您可以编写一个bash脚本来完成这项工作,不过您能否解释一下为什么要这样做?听起来您应该禁用web服务器上的目录索引。正如@nullability所说,您最好编辑web服务器配置,如果使用共享主机,则最好使用.htaccess。您可能希望使用
选项+FollowSymLinks-index
创建
.htaccess
,这样用户就看不到您的文件。我用scandir尝试过,但我不知道是否有更好或更快的方法。如果没有当前的index.php,我想为每个文件夹生成一个相同的默认主页。非常感谢这正是我所需要的;)