Create file and folders recursively

前端 未结 3 831
轻奢々
轻奢々 2021-01-11 14:03

I got an array containing path names and file names

[\'css/demo/main.css\', \'home.css\', \'admin/main.css\',\'account\']

I want to create

3条回答
  •  佛祖请我去吃肉
    2021-01-11 14:58

    I have just used a simple way to explode the string and rebuild and check if is a file or a directory

    public function mkdirRecursive($path) {
    
    
        $str = explode(DIRECTORY_SEPARATOR, $path);
        $dir = '';
        foreach ($str as $part) {
            $dir .= DIRECTORY_SEPARATOR. $part ;
            if (!is_dir($dir) && strlen($dir) > 0 && strpos($dir, ".") == false) {
                mkdir($dir , 655);
            }elseif(!file_exists($dir) && strpos($dir, ".") !== false){
               touch($dir);
            }
        }
    }
    

提交回复
热议问题