在此脚本中,如何将10MB的限制更改为5 GB?

时间:2017-12-29 作者:Darren Cain

找到了这个,它可以工作,但要将10 MB限制更改为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 );
}
此函数在上载文件之前运行
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 个回复
最合适的回答,由SO网友:Darren Cain 整理而成

我让它工作了。我删除了脚本的上半部分。不知道为什么它现在能工作,但测试了它,并做了我想要的。感谢您的帮助和支持。可以找到原始线程here, 对于那些感兴趣的人。

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;
}

SO网友:Nicolai Grossherr

你必须设置PHP upload limits 正确地

以字节为单位计算5 GB将如下所示:

$5GBinBytes = 1024 * 1024 * 1024 * 5;
// or nicer
$5GBinBytes = pow( 1024, 3 ) * 5;
5GB是大量数据,这可能需要一些时间。您很有可能必须加快流程时间限制——max_execution_timemax_input_time 浮现在脑海中–以及memory_limit

根据@swissspidy的注释,WordPress定义了常量GB_IN_BYTES, 用法:

$5GBinBytes = GB_IN_BYTES * 5;

结束

相关推荐

Limit tag word count

我想限制用户只输入两个而不是更多的post\\u tag单词。如果任何标签超过两个单词,用户将不会发布帖子。我需要用这个函数数数单词。function count_words( $text ) { $text2 = preg_replace( \'/<.[^<>]*?>/\', \' \', $text ); $text2 = preg_replace( \'/&nbsp;|&#160;/i\', \' \', $text2 );&#