2012-12-30 96 views
3

我必須創建遞歸目錄和創建的每個文件夾中的文件。我想是這樣的:PHP的遞歸目錄創建

 
    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 

但這個代碼生成這樣的:

 
    |-- 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 

代碼是:

protected function createFiles($files, $parent_directory = null) 
    { 
     echo $parent_directory."\n</br>\n"; 
     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; 
    } 

之所以不使用遞歸的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); 

謝謝。

+1

你爲什麼不使用PHP的的mkdir()函數的遞歸選項? –

+0

因爲想要的目錄是一個類似於問題結束的數組。 (我會加一分鐘。) – delirehberi

回答

1

我想用遞歸的MKDIR仍然是最好的主意,所有你需要做的是Concat的嵌套數組鍵:

$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 
     } 

    } 
1

看看命令touch

如果你有,你必須創建(沒有內容)的文件集列表,你可以遍歷您的數組調用它。

你需要改變你的代碼提供文件名的一維數組。

$dirs = array(); 
$dirs[] = "modules\default\$alias\a\$alias.php"; 
... 
$dirs[] = "templates\default\$alias\$alias.tpl"; 

foreach($dirs as $file) { 
    touch($file); 
} 

注意,有許可有關的限制,所以讀了觸摸,如果你發現沒有被創建的文件。

另外值得注意的是,你缺少在你的例子fclose()。你應該看看file_put_contents(),因爲它爲你處理fopen/fwrite/fclose

+0

謝謝你,我這樣做: $ dirs = array(); $ dirs ['modules/default /'。別名。'/ a'] = $別名。 「.PHP」; $ dirs ['modules/default /'。別名。'/ m'] = $別名。 '.PHP'; $ dirs ['modules/default /'。別名。'/ c'] = $別名。 '.PHP'; $顯示目錄[ '模塊/根/'.$別名。'/ A'] = $別名。 '.PHP'; $ dirs ['modules/root /'。別名。'/ m'] = $別名。 '.PHP'; $ dirs ['modules/root /'.$ alias。'/ c'] = $別名。 '.PHP'; $ dirs ['templates /root/'.$別名] = $別名。 '.tpl'; $ dirs ['templates/default /'.$ alias] = $別名。 '.tpl'; – delirehberi

0

您可以嘗試

class Maker { 

    function createFiles($config, $path = null) { 
     foreach ($config as $k => $v) { 
      if (is_array($v)) { 
       mkdir($path . DIRECTORY_SEPARATOR . $k, 0777); 
       chmod($path . DIRECTORY_SEPARATOR . $k, 0777); 
       $this->createFiles($v,$path . DIRECTORY_SEPARATOR . $k); 
      } 
      else 
      { 
       touch($path . DIRECTORY_SEPARATOR . $v); //create the file 
      } 
     } 
    } 
} 

$alias = "test_module"; 
$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'; 

$maker = new Maker(); 
$maker->createFiles($dirs, __DIR__ . "/test"); 
+0

問題不是創建文件。文件創建是正確的,但目錄是錯誤的。可能parent_directory是更改,但其中:\ – delirehberi

0

我的決心:

protected function createModuleFiles($alias){ 
     //Default kısmına php controller dosyası oluşturulacak. 
     $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); 

    } 

protected function createFiles($files){ 
     foreach($files as $key=>$value){ 
      if(!is_dir($key)){ 
       mkdir(www.$key,0777,true); 
      } 
      chmod(www.$key,0777); 
      $alias = explode('.',$value); 
      $alias = $alias[0]; 
      $type = end(explode('/',$key)); 
      switch($type){ 
       case 'a'; 
       case 'c'; 
       case 'm'; 
        $content = $this->default_contents($alias,$type); 
       break; 
       default: 
        $content = $this->default_contents($alias,'tpl'); 
        break; 
      } 
      $file = fopen(www.$key.'/'.$value, 'w+'); 
      $write = fwrite($file, $content); 
      if (!$write) { 
       throw new AjaxCatcher("".www.$key.'/'.$value." an error. "); 
      } 

     } 

     return 1; 
    }