0

Found this and it works, but want to change the 10 MB limit to 5 GB.

$upload_bytes_limit_reached = ( ( $filesize + $upload_bytes ) > ( 1024 * 1024 * 10 ) );

add_filter( 'wp_handle_upload', 'wpse47580_update_upload_stats' );
function wpse47580_update_upload_stats( $args ) {
    $size = filesize( $args['file'] );

    $user_id = get_current_user_id();

    $upload_count = get_user_meta( $user_id, 'upload_count', true );
    $upload_bytes = get_user_meta( $user_id, 'upload_bytes', true );

    update_user_meta( $user_id, 'upload_count', $upload_count + 1 );
    update_user_meta( $user_id, 'upload_bytes', $upload_bytes + $size );
}

This function runs before the file is uploaded.

add_filter( 'wp_handle_upload_prefilter', 'wpse47580_check_upload_limits' );
function wpse47580_check_upload_limits( $file ) {
    $user_id = get_current_user_id();

    $upload_count = get_user_meta( $user_id, 'upload_count', true );
    $upload_bytes = get_user_meta( $user_id, 'upload_bytes', true );

    $filesize = $file['size']; // bytes

    $upload_bytes_limit_reached = ( ( $filesize + $upload_bytes ) > ( 1024 * 1024 * 10 ) );

    $upload_count_limit_reached = ( $upload_count + 1 ) > 100;

    if ( $upload_count_limit_reached || $upload_bytes_limit_reached )
        $file['error'] = 'Upload limit has been reached for this account!';

    return $file;
}

2 Answers 2

2

You have to set PHP upload limits properly.

Calculating 5 GB in Bytes would look like:

$5GBinBytes = 1024 * 1024 * 1024 * 5;
// or nicer
$5GBinBytes = pow( 1024, 3 ) * 5;

5GB is a lot of data, which might take some time. There is a good chance you have to crank up process time limits – max_execution_time and max_input_time come to mind – and memory_limit as well.

As per comment by @swissspidy, WordPress defines the constant GB_IN_BYTES, usage:

$5GBinBytes = GB_IN_BYTES * 5;
9
  • processing won't be affected unless php's limit for single uploads is increased.
    – inarilo
    Dec 30, 2017 at 5:00
  • my understanding was that he just wants to increase the total (application controlled) limit and not PHP upload limits that affect single uploads
    – inarilo
    Dec 30, 2017 at 17:52
  • @inarilo Ah maybe you're right, let's wait what he says. Dec 30, 2017 at 18:03
  • I tried that, but then when i test and upload of 29 MB, it shows the image missing in wordpress and no info. All php limits are high, as stated in your link. For the 5 GB, I want it so its the maximum in total they can upload to their site, not max size in a single file upload. Dec 31, 2017 at 17:16
  • 2
    FYI, WordPress defines the GB_IN_BYTES constant, so one can simply use GB_IN_BYTES * 5 to describe 5 GB.
    – swissspidy
    Jan 17, 2018 at 13:34
0

I have it working. I removed the top half of the script. Don't know why it now works, but tested it and does what I wanted. Thanks for all your help and support. The original thread can be found here, for those interested.

add_filter( 'wp_handle_upload_prefilter', 'wpse47580_check_upload_limits' );
function wpse47580_check_upload_limits( $file ) {
    $user_id = get_current_user_id();

    $upload_bytes = get_user_meta( $user_id, 'upload_bytes', true );

    $filesize = $file['size']; // bytes

    $upload_bytes_limit_reached = ( ( $filesize + $upload_bytes ) > ( GB_IN_BYTES * 5 ) );

    if ( $upload_bytes_limit_reached )
        $file['error'] = 'Upload limit has been reached for this account!';

    return $file;
}
2
  • Nice, you figured it out. I came back late to this, because of the weekend. Just because you asked me in the comment, I didn't try the script, because your question was how to increase the limit, and as I said, if it's working with 10MB then it's also working with GB. Anyhow, you did it yourself, which is more rewarding anyway :) Jan 21, 2018 at 20:47
  • @Nicolai It took some time, but got there. Thanks for your help tho, appreciate yours and others suggestions. :) Jan 22, 2018 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.