Add files to folders using PHP

I have over 100 folders with subfolders ...

Example:

a / b / c / d / e
f / g / h
m / n
k / l / o / p / s / z / x /

....

I want to add an empty file (index.html) to each folder (and each subfolder). Can I do this using PHP? And How?

+3
source share
4 answers

Could be simpler with a shell script:

exec('
  for DIR in $(find . -type d) ; do touch "$DIR/index.html" ; done
');

With PHP, more effort:

function scandir_tree($dir) {
    $r = array("$dir");
    foreach (scandir($dir) as $fn) {
        if (is_dir("$dir/$fn") && ($fn[0] != ".")) {
            $r = array_merge($r, scandir_tree("$dir/$fn"));
        }
    }  
    return $r;
}

foreach (scandir_tree(".") as $dir) {
    touch("$dir/index.html");
}

Or as said, using single Options -Indexthrough.htaccess to prevent the creation of a default directory list with Apache.

+3
source

Yes, take a look at recursion and directory / file commands.

Take the top directory (s), go through them, if necessary, only if their directory name matches az, etc. Then open the index.html file and save it (or copy it from the previous copy)

0

, :

  • index.html file_exists
  • scandir
  • , ( is_dir)
  • .
0

You probably need a good tutorial on writing files: Rank and files

and open dir

save the folder names in an array and pass them through one file at a time. or opendir can provide you with a list of folders and just loop and write files

0
source

All Articles