-1

Is there a way to check if index.php exists in my subdirectories ?

For instance :

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

I want to check if there is a index.php in folder, folder2, folder3. Eventually, I should create index.php if it doesn't exist.

Tom
  • 3
  • 2
  • 1
    What have you tried so far? Take a look at is_file,is_dir,glob and examples of recursive glob functions on the doc page: http://us3.php.net/manual/en/function.glob.php – oliakaoil Jul 18 '14 at 20:05
  • I'm sure you could write a bash script to do this for you, although could you explain why you want to do this? It sounds like you should just disable directory indexes on your web server. – nullability Jul 18 '14 at 20:06
  • As @nullability says, you'd properly be better of editing your webserver config, or use .htaccess if using a shared host. http://stackoverflow.com/questions/1767785/htaccess-file-options-indexes-on-subdirectories – Jono20201 Jul 18 '14 at 20:07
  • you would want to make a `.htaccess` with `Options +FollowSymLinks -Indexes` so users can't see your files – cmorrissey Jul 18 '14 at 20:09
  • I tried with scandir but i don't know if there is a better or faster way. I want to generate a same default homepage for each folder if there is no current index.php. – Tom Jul 18 '14 at 20:09
  • Okay, what problem are you trying to solve by putting `index.php` everywhere? – Yang Jul 18 '14 at 20:10
  • By the way, this approach keeping folders secure is old-fashioned and outdated. People were using it in 2002-2008 – Yang Jul 18 '14 at 20:12
  • sure. get a list of your subdirectories, iterate over them, and check for index.php in each. – Marc B Jul 18 '14 at 20:13
  • Yeah i know, this is not what i'm trying to do haha. I just want to generate the same index.php which will behave differently depending on the current directory. But first i want to check if there is a index.php – Tom Jul 18 '14 at 20:20

1 Answers1

1

Wrote a quick class that would do this, run from command prompt/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;
Jono20201
  • 3,215
  • 3
  • 20
  • 33