以下代码:
$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
只需将迭代器转换为具有
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"; } }
原文地址:https://www.jb51.cc/php/133803.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。