Documentation

Too many open files error

WP Offload Media may display a “Too many open files” error notice when attempting to offload media files to your storage provider.

Offload Errors - too many open files

What Does This Error Mean?

This error message is a result of the server running out of processing resources. We generally see this error on lower-tier web hosting packages when first offloading a large number of media files. Because processing resources on these web hosts are typically shared, it means that there is not enough processing power available to offload all your media files.

What can I do about it?

As you’re not able to change your processing resources, we do have a workaround to fix this problem. It involves using our Tweaks plugin, installing it to your site, and making a couple of changes to the code.

If you’ve never edited a plugin, then the most straightforward way to make these changes is to do the following:

  • Upload the plugin manually via the WordPress dashboard but don’t activate it once it’s uploaded.
  • Open the WordPress Plugin Editor by navigating to Plugins > Plugin Editor.
  • Click I understand on the notice that pops up, then select the “WP Offload Media Tweaks” plugin from the “Select plugin to edit” dropdown and click the Select button
  • The editor window will show the code for the tweaks plugin, and you can make the necessary changes there.

Edit Tweaks Plugin

The first change to make is to uncomment the line which looks like this:

//add_filter( 'as3cf_pre_upload_attachment', array( $this, 'pre_upload_attachment' ), 10, 3 );

Remove the two slashes (//) to uncomment that line.

add_filter( 'as3cf_pre_upload_attachment', array( $this, 'pre_upload_attachment' ), 10, 3 );

Then, scroll down to the pre_upload_attachment function, which looks like this:

function pre_upload_attachment( $abort, $post_id, $metadata ) {
    // Example stops movie files from being offloaded.
    $file      = get_post_meta( $post_id, '_wp_attached_file', true );
    $extension = is_string( $file ) ? pathinfo( $file, PATHINFO_EXTENSION ) : false;
    if ( is_string( $extension ) && in_array( $extension, array( 'mp4', 'mov' ) ) ) {
        $abort = true; // abort the upload
    }

    // Example helps bulk offload tool on severely resource restricted shared hosting.
    // WARNING: Do not uncomment the following code unless you're on shared hosting and getting "too many open files" errors
    // as `gc_collect_cycles()` could potentially impact performance of the bulk offload and WordPress.
    /*
    if ( false === $abort ) {
        gc_collect_cycles();
    }
    */

    return $abort;
}

Comment out the part of the function that prevents offloading of movie files. As you will be commenting out a code block, you will use the multi-line comment characters / and /.

Insert a line with the opening comment character /* just before this line:

// Example stops movie files from being offloaded.

Then, add the closing comment character */ after the if statement’s closing curly bracket }. Once you have made your changes, the function should look like this:

function pre_upload_attachment( $abort, $post_id, $metadata ) {
    /*
    // Example stops movie files from being offloaded.
    $file      = get_post_meta( $post_id, '_wp_attached_file', true );
    $extension = is_string( $file ) ? pathinfo( $file, PATHINFO_EXTENSION ) : false;
    if ( is_string( $extension ) && in_array( $extension, array( 'mp4', 'mov' ) ) ) {
        $abort = true; // abort the upload
    }
    */

    // Example helps bulk offload tool on severely resource restricted shared hosting.
    // WARNING: Do not uncomment the following code unless you're on shared hosting and getting "too many open files" errors
    // as `gc_collect_cycles()` could potentially impact performance of the bulk offload and WordPress.

    /*
    if ( false === $abort ) {
        gc_collect_cycles();
    }
    */

    return $abort;
}

Your next step is to uncomment the section just under the part where it says the following:

WARNING: Do not uncomment the following code unless you're on shared hosting and getting "too many open files" errors`

That code block is already commented out using multi-line comment characters / and /. Remove those characters and it should end up looking like this:

function pre_upload_attachment( $abort, $post_id, $metadata ) {
    /*
    // Example stops movie files from being offloaded.
    $file      = get_post_meta( $post_id, '_wp_attached_file', true );
    $extension = is_string( $file ) ? pathinfo( $file, PATHINFO_EXTENSION ) : false;
    if ( is_string( $extension ) && in_array( $extension, array( 'mp4', 'mov' ) ) ) {
        $abort = true; // abort the upload
    }
    */

    // Example helps bulk offload tool on severely resource restricted shared hosting.
    // WARNING: Do not uncomment the following code unless you're on shared hosting and getting "too many open files" errors
    // as `gc_collect_cycles()` could potentially impact performance of the bulk offload and WordPress.

    if ( false === $abort ) {
        gc_collect_cycles();
    }

    return $abort;
}

At the very bottom of the code editor window, click the Update File button, which will save the plugin file. Once you’ve saved the plugin, you can activate it from the plugins list.

Your media files should now offload to your chosen storage provider, although the process will take slightly longer. Once the bulk offload has finished, you can deactivate the Tweaks plugin or delete it from your site entirely.