WordPress功能:限制大小,仅限jpg、jpeg、每个帐户的上传文件限制

时间:2014-02-05 作者:c64girl

需要有关限制非管理员上传功能的帮助:

上载前检查文件是否为jpg、jpeg我正在搜索能够做到这一点但运气不佳的插件或函数:(

在wordpress中上传之前是否可以检查文件?

1 个回复
SO网友:fischi

由于WordPress是服务器端脚本,因此无法在上载之前检查图像。

但是,在将图像插入媒体库之前,您有不同的选项,如中所述this answer, 带筛选wp_handle_upload_prefilter.

在您的情况下,函数将过滤wp_handle_upload_prefilter 应该是这样的:

add_filter(\'wp_handle_upload_prefilter\', \'f711_image_size_prevent\');
function f711_image_size_prevent($file) {

    // get filesize of upload
    $size = $file[\'size\'];
    $size = $size / 1024; // Calculate down to KB

    // get imagetype of upload
    $type = $file[\'type\'];
    $is_image = strpos($type, \'image\');

    // set sizelimit
    $limit = 700; // Your Filesize in KB

    // set imagelimit
    $imagelimit = 7;

    // set allowed imagetype
    $imagetype = \'image/jpeg\';

    // query how many images the current user already uploaded
    global $current_user;
    $args = array(
        \'orderby\'         => \'post_date\',
        \'order\'           => \'DESC\',
        \'numberposts\'     => -1,
        \'post_type\'       => \'attachment\',
        \'author\'          => $current_user->ID,
    );
    $attachmentsbyuser = get_posts( $args );

    if ( ( $size > $limit ) && ($is_image !== false) ) { // check if the image is small enough
        $file[\'error\'] = \'Image files must be smaller than \'.$limit.\'KB\';
    } elseif ( $type != $imagetype ) { // check if image type is allowed
        $file[\'error\'] = \'Image must be \' . $imagetype . \'.\';
    } elseif ( count( $attachmentsbyuser ) >= $imagelimit ) { // check if the user has exceeded the image limit
        $file[\'error\'] = \'Image limit of \' . $imagelimit . \' is exceeded for this user.\';
    }
    return $file;

}

结束

相关推荐

如何使用图片上传的getimageSize()?

我需要通过media box检查要上载的图像的分辨率,然后将其上载到自定义目录。作为起点,我使用了How Can I Organize the Uploads Folder by Slug (or ID, or FileType, or Author)?其中一部分是这样的:function wpse_25894_custom_upload_dir($path) { $use_default_dir = ( isset($_REQUEST[\'post_id\'] )