简体   繁体   中英

Create subdirectories within each existing directory by php

Need to create three (3) directories within each, already existing subdirectories (but not in any sub-subdirectories or deeper). The name of subdirectories varies and there is no consensus in the naming. PHP Windows Command Line.

Start:

C:/aaa         //this is the current working directory
C:/aaa/001     //subdirectory 001
C:/aaa/002
C:/aaa/ccc/xyz
C:/aaa/randomname
.
C:/aaa/z

Result:

C:/aaa
C:/aaa/001/dir1
          /dir2
          /dir3
C:/aaa/002/dir1
          /dir2
          /dir3
C:/aaa/ccc/dir1
          /dir2
          /dir3
C:/aaa/ccc/xyz
C:/aaa/randomname/dir1
                 /dir2
                 /dir3
.
C:/aaa/z/dir1
        /dir2
        /dir3

How to modify this php code so this takes place in each first level subdirectory?

mkdir("dir1", 0700);
mkdir("dir2", 0700);
mkdir("dir3", 0700);

Just modify the arrays in which directories the folders should be created, and what folders

<?php
    $create_directoires = ["dir1", "dir2", "dir3"];
    $exclude_directories = [".", ".."];

    $dh = opendir(getcwd());

    while(($file = readdir($dh)) !== false) {
        if(is_dir($file) && !in_array($file, $exclude_directories)) {
            foreach($create_directoires as $create_directory) {
                if(!file_exists($file."/".$create_directory)) {
                    mkdir($file."/".$create_directory, 0700);
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM