![](/img/trans.png)
[英]Recursively adding all sub directories and files from a directory into an array
[英]PHP: recursively remove brackets from filenames in directory and all sub directories
我在删除系统中上传文件中的括号时遇到问题。
我能够删除单个目录的文件中的括号,但我正在努力使其在同一文件夹的子目录中递归工作。
我正在使用 str_replace 来查找括号 [ ] 并将它们替换为空白字符。
$dir = $_SERVER["DOCUMENT_ROOT"]."/uploads" ; // what directory to parse
if ( $handle = opendir ( $dir)) {
print "Directory Handle = $handles\n" ;
print "Files : \n" ;
while ( false !== ($file = readdir($handle))) {
if ( $file != "." && $file != ".." ) {
$isdir = is_dir ( $file ) ;
if ( $isdir == "1" ) {} // if its a directory do nothing
else {
$file_array[] = "$file" ; // get all the file names and put them in an array
print "$file\n" ;
} // closes else
} // closes directory check
} // closes while
} // closes opendir
//Lets go through the array
$arr_count = count( $file_array ) ; // find out how many files we have found so we can initiliase the counter
for ( $counter=1; $counter<$arr_count; $counter++ ) {
print "Array = $file_array[$counter]\n" ; // tell me how many files there are
$illegals = array("[","]");
$new = str_replace ( $illegals, "", $file_array[$counter] ) ; // now create the new file name
$ren = rename ( "$dir/$file_array[$counter]" , "$dir/$new" ) ; // now do the actual file rename
print "$new\n" ; // print out the new file name
}
closedir ( $handle ) ;
echo $dir;
好的,所以我从递归重命名脚本中获取了代码并创建了我自己的清理脚本。 如果我搞砸了什么,请告诉我。 感谢您为我指明正确的方向 NIC3500
$path = $_SERVER["DOCUMENT_ROOT"]."/uploads" ;
$illegals = array("[","]");
$di = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach($di as $name => $fio) {
//$newname = $fio->getPath() . DIRECTORY_SEPARATOR . strtolower( $fio->getFilename() );
$newname = $fio->getPath() . DIRECTORY_SEPARATOR . str_replace ( $illegals, "", $fio->getFilename() );
//echo $newname, "\r\n";
rename($name, $newname);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.