2017-10-31 13 views
-4

システム内のアップロードファイルに大括弧を取り除く際に問題があります。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; 
+1

"私は現在コードを見つけることができませんが、後でアップロードします。" OK、私は待つよ... – nogad

+1

[PHP:ディレクトリ内のすべてのファイルの名前を再帰的に小文字に変更する](https://stackoverflow.com/questions/32173320/php-rename-all-files-to) -low-case-in-a-directory-recursively) – Nic3500

+0

すぐにコードをアップロードしないと申し訳ありません。それはすでに午前2時、私のGFは私に叫んでいた。これを行う方法を見つけるために一日中過ごした。 – feratechinc

答えて

1

[OK]をので、私は、再帰的な名前変更スクリプトからコードを取り、自分のサニタイズスクリプトを作成しました。私が何かを犯したかどうか教えてください。正しい方向に私を指してくれてありがとう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); 

} 
関連する問題