рекурсия — PHP: рекурсивно удаляет скобки с имен файлов в каталоге и всех подкаталогах

У меня проблема с избавлением от скобок в загруженных файлах в системе.

Я могу удалить квадратные скобки в файлах одного каталога, но я изо всех сил пытаюсь заставить это работать рекурсивно в подкаталогах той же самой папки.

Я использую 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;

-4

Решение

Итак, я взял код из сценария рекурсивного переименования и создал свой собственный сценарий очистки. Пожалуйста, дайте мне знать, если я что-нибудь напортачил. Спасибо, что указали мне правильное направление 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);

}
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector