递归创建目录的一个函数

下面是从openid的php代码中看到的一个递归创建目录的函数,感觉是比较简洁的,值得参考

<?php
    function ensureDir($dir_name)                                                 
    {                                                                             
        if (
is_dir($dir_name) || @mkdir($dir_name)) {                             
            return 
true;                                                          
        } else {                                                                  
            
$parent_dir dirname($dir_name);                                     
                                                                                  
            
// Terminal case; there is no parent directory to create.             
            
if ($parent_dir == $dir_name) {                                       
                return 
true;                                                      
            }                                                                     
                                                                                  
            return (
ensureDir($parent_dir) && @mkdir($dir_name));    
        }                                                                         
    }  
?>

留下评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据