2011-08-22 4 views
7

다음 코드 :RecursiveDirectoryIterator는 "열려있는 파일이 너무 많습니다"에 UnexpectedValueException을 던져

: 당신이 반복하려는 하위 폴더에 너무 많은 파일이있는 경우

$zip = new ZipArchive(); 

if ($zip->open('./archive.zip', ZIPARCHIVE::CREATE) !== TRUE) { 
    die ("Could not open archive"); 
} 

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("./folder/")); 

foreach ($iterator as $key => $value) { 
    try { 
    $zip->addFile(realpath($key), $key); 
    echo "'$key' successfully added.\n"; 
    } catch (Exception $e) { 
    echo "ERROR: Could not add the file '$key': $e\n"; 
    } 
} 

$zip->close(); 

는 다음과 같은 예외가 발생합니다

Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(./some/path/): failed to open dir: Too many open files' in /some/other/path/zip.php:24 
Stack trace: 
#0 [internal function]: RecursiveDirectoryIterator->__construct('./some/path/') 
#1 /some/other/path/zip.php(24): RecursiveDirectoryIterator->getChildren() 
#2 {main} 
thrown in /some/other/path/zip.php on line 24 

이 예외가 발생하지 않고 어떻게 많은 양의 폴더와 파일을 성공적으로 반복 할 수 있습니까? 당신이 원하는만큼 파일을 반복 할 수처럼

답변

7

는 단순히 iterator_to_array 함수로 배열에 반복자를 변환하여, 그것은 보인다

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("./folder/")); 
$files = iterator_to_array($iterator, true); 

// iterate over the directory 
// add each file found to the archive 
foreach ($files as $key => $value) { 
    try { 
    $zip->addFile(realpath($key), $key); 
    echo "'$key' successfully added.\n"; 
    } catch (Exception $e) { 
    echo "ERROR: Could not add the file '$key': $e\n"; 
    } 
} 
관련 문제