dousha1394
dousha1394
2014-07-29 14:40

递归地大写给定目录PHP或命令行中的所有文件和目录

已采纳

I need to properly title case all the directories and files inside my application folder as I'm moving my app to PSR-0 with namespacing.

I'm sure there's an elegant way in either php or via the commandline (Mac OSX) to accomplish this without spending hours doing it.

My structure looks like so:

application
    admin
        controller
            dir1
                file.php
            dir2
                file.php
            ...
        language
            english
                english.php
                dir1
                    file.php
                    ...
                dir2
                    file.php
                ...
        model
            dir1
                file.php
                ...
            dir2
                file.php
                ...
            ...
    public
        ...

I tried using glob but that gives me the full file paths and doesn't seem to change the actual directory name or file name.

Any help appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douzhuo1853 douzhuo1853 7年前

    My suggestion is that you use the RecursiveDirectoryIterator. Documentation can be found here: http://php.net/manual/en/class.recursivedirectoryiterator.php

    <?php
    
    $path = realpath('PATH_TO_STARTING_DIRECTORY'); // replace with the real path
    
    $items = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), 
    RecursiveIteratorIterator::SELF_FIRST);
    
    // Loop through first time & change files
    foreach($items as $name => $f){
        if (!is_dir($f)) {
            // If you want to be all caps use
            strtoupper(trim($f));
    
            // If you want to be only first letter caps
            ucwords(trim($f));
        }
    }
    
    // Loop through a second time & change directories
    foreach($items as $name => $d) {
        if (is_dir($d) && ('.' != $d && '..' != $d)) {
            // If you want to be all caps use
            strtoupper(trim($d));
    
            // If you want to be only first letter caps
            ucwords(trim($d));
        }
    }
    ?>
    
    点赞 评论 复制链接分享

相关推荐