2011-08-22 13 views
7

Aşağıdaki kod:RecursiveDirectoryIterator "Çok fazla açık dosya" konulu UnexpectedValueException atar

: Eğer tekrarlatacak çalışıyoruz bir alt klasörde çok fazla dosya varsa

$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(); 

aşağıdaki istisna atar

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 

Bu özel durum yaşanmadan büyük miktarda klasör ve dosya üzerinde nasıl başarılı bir şekilde yineleyebilirsiniz? İstediğiniz kadar çok dosya adımlayabilirsiniz gibi

cevap

7

Basitçe iterator_to_array fonksiyonu ile bir diziye yineleyici dönüştürerek, öyle görünüyor:

İlgili konular