简体   繁体   中英

How to decrease file size upload for non-admin users?

My site allows upload file size of up to 256 MB but I would like to limit my site users to only 1 MB max and keep the maximum 256 MB for the admins. All solutions I found only show how to increase the upload limit, but not decrease for specific roles.

I tried the following but it didn't work:

function increase_upload_size_limit( $limit ) {
  if ( ! current_user_can( 'manage_options' ) ) {
    $limit = 1048576; // 1 MB
  }
  return $limit;
}
add_filter( 'upload_size_limit', 'increase_upload_size_limit' );

This seems like what you're asking for is just an if/else?

function increase_upload_size_limit( $limit ) {
  if ( ! current_user_can( 'manage_options' ) ) {
    $limit = 1048576; // 1 MB
  } else {
      $limit = 268435456;
  }
    return $limit;
}
add_filter( 'upload_size_limit', 'increase_upload_size_limit' );

you should try something like this:

$maxsize = 1048576;
if(($_FILES['uploaded_file']['size'] >= $maxsize) || ($_FILES["uploaded_file"]["size"] == 0)) {
        $errors[] = 'File too large. File must be less than 1 megabyte.';
    }

define $maxsize based on user logged in

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM