为用户提供最大上传容量;限制用户可以上传的文件数量或限制每次上传的文件数量

时间:2012-04-01 作者:turbonerd

我正在使用我网站前端的媒体库,我想通过上传无限数量的文件来阻止用户向我的服务器发送垃圾邮件。

因此,我想做以下一项或全部工作:

为用户提供最大上传容量;i、 e.用户可以上传多达10兆字节的文件

提前感谢,

2 个回复
最合适的回答,由SO网友:soulseekah 整理而成

假设您通过WordPress的本机功能提供上传功能,例如wp_handle_upload 或者更高层次的东西,我们得出结论,几个钩子将被拉。

http://core.trac.wordpress.org/browser/tags/3.3/wp-admin/includes/file.php#L212

这个wp_handle_upload 函数可能是最后一个接触该文件的本机函数,并且知道跟踪所需的所有信息。

此函数中有两个挂钩:wp_handle_uploadwp_handle_upload_prefilter. 后者是第一位的,这可能会检查当前限制并阻止文件上载。前者将跟踪文件大小和计数。存储信息将由update_user_meta.

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

    $user_id = get_current_user_id();

    $upload_count = get_user_meta( $user_id, \'upload_count\', $single = true );
    $upload_bytes = get_user_meta( $user_id, \'upload_bytes\', $single = 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\', $single = true );
    $upload_bytes = get_user_meta( $user_id, \'upload_bytes\', $single = true );

    $filesize = /* get filesize from $file array */;
    $upload_bytes_limit_reached = apply_filters( \'wpse47580_upload_bytes_limit_reached\', 1024*1024*10 ) > ( $filesize + $upload_bytes );
    $upload_count_limit_reached = apply_filters( \'wpse47580_upload_count_limit_reached\', 100 ) > ( $upload_count + 1 );

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

    return $file;
}
理论上,这是可行的;几乎没有经过测试。让我们知道进展如何。

每个帖子的上传限制将保留在帖子元中,可能类似于{$user_id}_upload_count 等等,我不明白为什么这样做行不通。

如果您使用自定义代码来处理上传(我怀疑),那么您可以实现自己的操作和过滤器,就像wp_handle_uploads

SO网友:turbonerd

我修改了Soulseekah的代码apply_filter 变量对我不起作用-可能是因为我不理解它们!

# [File Upload]
#
# Two filters to give users a maximum upload limit of 10Mb and 100 files.
# This function runs after the file has been uploaded.
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;
}
这对于制作插件来说非常简单,所以我可能会在将来的某个时候发布它,当我为它开发了一个接口时。

结束

相关推荐

404从wp-Content/Uploads/获取图像时

我在获取图像时获得404状态,http仍然包含该图像。图像显示在浏览器中,但404代码中断了一些应用程序。对wp内容/上载/的调用被重定向到。htaccess:<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\\.php$ - [L] RewriteRule (.*) /index.php?getfile=$1 [L] </IfModule>&