[英]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.