Search code examples
phpdownloadstream

How to fix `failed to open stream: Too many open files error` in PHP


I have a loop where I save some images locally, then move them to S3

foreach ($images as $image) {
    // download image
    $stream = fopen($image['path'], 'wb');
    write($stream, $image['content']);
    fclose($stream);

    // copy to S3
    $manager->copy('local://' . $image['p'], 's3://' . $image['p']);
}

The problem is that I get a failed to open stream: Too many open files error error

Any ideas how can I avoid this?

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

Solution

  • Increase the ulimit -n or disable multipart uploading inside the aws-s3 driver for flysystem. it is flysystem that is keeping too much files open.

    Ok a quick fix for you would be:

    foreach ($images as $image) {
    
       $resourcesOpened = count(get_resources('stream'));
    
         while ( $resourcesOpened > 900 ) {
           sleep(10);
           $resourcesOpened = count(get_resources('stream'));
         }
    
         // download image
         $stream = fopen($image['path'], 'wb');
         write($stream, $image['content']);
         fclose($stream);
    
         // copy to S3
         $manager->copy('local://' . $image['p'], 's3://' . $image['p']);
       }
    
    
    }