简体   繁体   English

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

[英]Recursively Capitalize all Files and Directories within a Given Directory PHP or Commandline

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. 我需要使用命名空间将应用程序移至PSR-0时,对应用程序文件夹中的所有目录和文件正确命名。

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. 我敢肯定,在php或通过命令行(Mac OSX)中有一种优雅的方法可以完成此任务,而无需花费数小时的时间。

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. 我尝试使用glob,但这给了我完整的文件路径,并且似乎没有更改实际的目录名称或文件名。

Any help appreciated. 任何帮助表示赞赏。

My suggestion is that you use the RecursiveDirectoryIterator. 我的建议是您使用RecursiveDirectoryIterator。 Documentation can be found here: http://php.net/manual/en/class.recursivedirectoryiterator.php 可以在这里找到文档: http : //php.net/manual/zh/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));
    }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 PHP 为目录中的所有 JavaScript 文件递归生成脚本标签 - Recursively generate script tags for all JavaScript files within a directory with PHP PHP递归函数:获取目录及其子目录中的所有文件和目录 - PHP Recursive function: Get all files and directories within a directory and its sub-directories 将目录中的所有子目录和文件递归添加到数组中 - Recursively adding all sub directories and files from a directory into an array PHP:从目录和所有子目录中的文件名中递归删除括号 - PHP: recursively remove brackets from filenames in directory and all sub directories 递归删除目录中具有不同名称的文件-PHP - Recursively delete files with different names within directories - PHP 递归 chmod/chown/chgrp 目录中的所有文件和文件夹 - Recursively chmod/chown/chgrp all files and folder within a directory 递归返回文件夹子文件夹中除目录以外的所有文件的列表PHP - Return the list of all files excluding directory in the folders subfolders recursively PHP PHP:递归地将所有文件重命名为目录中的小写 - PHP: rename all files to lower case in a directory recursively 递归列出目录中的所有文件 - Recursively listing all files in directory 在php中递归搜索所有目录中的字符串数组 - Recursively search all directories for an array of strings in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM