donglong7338 2012-12-30 20:26
浏览 78
已采纳

Php递归目录创建

I must create recursive directories and create a file for every folder. I want like this:

   modules/
   modules/default
   modules/default/test_module
   modules/default/test_module/a
   modules/default/test_module/a/test_module.php
   modules/default/test_module/m
   modules/default/test_module/m/test_module.php
   modules/default/test_module/c
   modules/default/test_module/c/test_module.php
   modules/root
   modules/root/test_module
   modules/root/test_module/a
   modules/root/test_module/a/test_module.php
   modules/root/test_module/m
   modules/root/test_module/m/test_module.php
   modules/root/test_module/c
   modules/root/test_module/c/test_module.php
   templates
   templates/default
   templates/default/test_module
   templates/default/test_module/test_module.tpl
   templates/root
   templates/root/test_module
   templates/root/test_module/test_module.tpl

But this code generate like this:

   |-- modules
   |   |-- default
   |   |   |-- root
   |   |   |   `-- test_module
   |   |   |       |-- a
   |   |   |       |   `-- test_module.php
   |   |   |       |-- c
   |   |   |       |   `-- test_module.php
   |   |   |       `-- m
   |   |   |           `-- test_module.php
   |   |   `-- test_module
   |   |       |-- a
   |   |       |   `-- test_module.php
   |   |       |-- c
   |   |       |   `-- test_module.php
   |   |       `-- m
   |   |           `-- test_module.php
   |   |-- root
   |   |   |-- index
   |   |   |   `-- c
   |   |   |       `-- index.php
   |   |   |-- modules
   |   |   |   |-- a
   |   |   |   |   `-- modules.php
   |   |   |   |-- c
   |   |   |   |   `-- modules.php
   |   |   |   `-- m
   |   |   |       `-- modules.php
   |   |   `-- user
   |   |       |-- a
   |   |       |   `-- user.php
   |   |       |-- c
   |   |       |   `-- user.php
   |   |       `-- m
   |   |           `-- user.php
   |   `-- templates
   |       `-- root
   |           |-- default
   |           |   `-- test_module
   |           |       `-- test_module.tpl
   |           `-- test_module
   |               `-- test_module.tpl

Code is:

    protected function createFiles($files, $parent_directory = null)
    {
        echo $parent_directory."
</br>
";
        if (!$parent_directory) {
            $parent_directory = www;
        }
        foreach ((array)$files as $key => $value) {
            if (is_array($value)) {
                if (!is_dir($parent_directory . $key)) {
                    mkdir($parent_directory . $key,0777);
                    chmod($parent_directory.$key,0777);
                }
                $parent_directory .= $key . '/';
                $this->createFiles($value, $parent_directory);
            } else {
                $parent_directory_=$parent_directory.$key . '/';
                if(!is_dir($parent_directory_)){
                    mkdir($parent_directory_,0777);
                    chmod($parent_directory_,0777);
                }
                $alias = explode('.',$value);
                $alias = $alias[0];
                $defaultAjaxContent = <<<AJAX
    <?php
       class {$alias} extends ajax{
       /**
        * Autocreated
        **/
       public function initAjax(){

       }
   }
   ?>
   AJAX;
                $file = fopen($parent_directory_.$value, 'w+');
                $write = fwrite($file, $defaultAjaxContent);
                if (!$write) {
                    throw new AjaxCatcher("{$parent_directory_}{$value} oluşturulurken beklenmedik bir hata oluştu. Lütfen tekrar deneyiniz. ");
                }
            }
        }
        //$file = fopen($files['default_ajax']);
        return 1;
    }

Reason why not use recursive mkdir :

$dirs = array();
        $dirs['modules']['default'][$alias]['a'] = $alias . ".php";
        $dirs['modules']['default'][$alias]['m'] = $alias . '.php';
        $dirs['modules']['default'][$alias]['c'] = $alias . '.php';

        $dirs['modules']['root'][$alias]['a'] = $alias . '.php';
        $dirs['modules']['root'][$alias]['m'] = $alias . '.php';
        $dirs['modules']['root'][$alias]['c'] = $alias . '.php';

        $dirs['templates']['root'][$alias] = $alias . '.tpl';
        $dirs['templates']['default'][$alias] = $alias . '.tpl';

        $this->createFiles($dirs);

thanks.

  • 写回答

4条回答 默认 最新

  • douhui4831 2012-12-30 20:50
    关注

    I think the mkdir with recursive is still the best idea, all you need to do is to concat the nested array keys:

        $dirs = array();
        $dirs['modules']['default'][$alias]['a'] = $alias . ".php";
        $dirs['modules']['default'][$alias]['m'] = $alias . '.php';
        $dirs['modules']['default'][$alias]['c'] = $alias . '.php';
    
        $dirs['modules']['root'][$alias]['a'] = $alias . '.php';
        $dirs['modules']['root'][$alias]['m'] = $alias . '.php';
        $dirs['modules']['root'][$alias]['c'] = $alias . '.php';
    
        $dirs['templates']['root'][$alias] = $alias . '.tpl';
        $dirs['templates']['default'][$alias] = $alias . '.tpl';
    
        // concat the keys (flatten the array)...
        function prefixKey($prefix, $array) {
            $result = array();
            foreach ($array as $key => $value) {
                if (is_array($value)) {
                    $result = array_merge($result, prefixKey($prefix . $key . '/', $value));
                } else {
                    $result[$prefix . $key] = $value;
                }
            }   
            return $result;
        }
    
        // this is your function, use the prefixKey function to get the required
        // pathstructure directly from your array...
        function createFiles($dirs) {
           $pathStructure = prefixKey('', $dirs);
           // here you have the flatten keys (the path), the value is the file
           foreach($pathStructure as $path => $file) {
                // use mkdir with 
                if(!is_dir($path)) {
                    mkdir($path, 0777, true);
                }
                // and so on
           }
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀