2017-10-31 2 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

"코드를 찾을 수 없지만 나중에 업로드 할 것입니다." 좋아, 그럼 기다릴게 ... – nogad

+1

[PHP : 모든 파일의 이름을 소문자로 재귀 적으로 이름을 바꿀 수 있음] (https://stackoverflow.com/questions/32173320/php-rename-all-files-to) -lower-case-in-a-directory-recursively) – Nic3500

+0

코드를 즉시 업로드하지 않으셔서 죄송합니다. 그것은 벌써 2am이었다. 그리고 나의 gf는 나에게 고함을 지르고 있었다. 하루 종일이 작업을 수행하는 방법을 찾으려고했습니다. – feratechinc

답변

1

은 그래서 재귀 이름 바꾸기 스크립트의 코드를 가져다가 내 자신의 위생적 스크립트를 만들었습니다. 내가 망쳤다면 알려줘. 올바른 방향으로 나를 가리켜 주셔서 감사합니다 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); 

} 
관련 문제