如果index.php存在于每个子目录中,我如何检查php

时间:2014-07-18 20:03:54

标签: php html

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

例如:

home/
|__folder/
|  |__index.php
|  |__style.css
|
|__folder2/
|  |__style.css
|
|__folder3/
   |__text.txt

我想检查文件夹,folder2,folder3中是否有index.php。 最后,如果它不存在,我应该创建index.php。

1 个答案:

答案 0 :(得分:0)

写一个快速的类来执行此操作,从命令提示符/终端运行。

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;