简体   繁体   English

如何减少非管理员用户上传的文件大小?

[英]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.我的网站允许上传文件大小高达 256 MB,但我想将我的网站用户限制为最大 1 MB,并为管理员保留最大 256 MB。 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?这似乎你所要求的只是一个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根据登录的用户定义$maxsize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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