title: How to fix `failed to open stream: Too many open files error` in PHP
body: <p>I have a loop where I save some images locally, then move them to S3</p>
  <pre><code>foreach ($images as $image) {
      // download image
      $stream = fopen($image['path'], 'wb');
      write($stream, $image['content']);
      fclose($stream);
  
      // copy to S3
      $manager-&gt;copy('local://' . $image['p'], 's3://' . $image['p']);
  }
  </code></pre>
  <p>The problem is that I get a <code>failed to open stream: Too many open files error</code> error</p>
  <p>Any ideas how can I avoid this?</p>
  <pre><code>Warning: include(/var/www/vendor/monolog/monolog/src/Monolog/Formatter/LineFormatter.php): failed to open stream: Too many open files in /var/www/vendor/composer/ClassLoader.php on line 444
  Fatal error: Uncaught Error: Class 'Monolog\Formatter\LineFormatter' not found in /var/www/vendor/symfony/monolog-bridge/Handler/ConsoleHandler.php:155
  </code></pre>