如何检查上传的文件是否是.pdf而不是.jpeg?

时间:2014-11-10 作者:Iurie

我使用a function 在我的主题中,阻止上载大尺寸图像文件(由于@fischi),但我想应用它strictly 到jpg/。仅限jpeg文件,因为它阻止上载。pdf文件也是。如何调整此功能?

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;

}

UPDATE

这是经过改进的代码(尚未测试),感谢@nikhil sheth:

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

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

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

    if ( $type == $imagetype ) {

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

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

        // set imagelimit
        $imagelimit = 7;

        // 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 ( 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;
    } else { return $file; }
}

3 个回复
SO网友:Howdy_McGee

我喜欢使用以下选项:

首先,我启动一个受支持mime类型的数组:

$supportedTypes = array( \'application/pdf\' );

接下来,我将获得实际上载的文件类型:

$fileType = $_FILES[\'type\'][0]

然后我使用wp_check_filetype() 要获取扩展名和mimetype,请执行以下操作:$fileArray = wp_check_filetype( basename( $_FILES[\'name\'][0] ) );

最后,我运行一个条件以确保上载的mimetype位于我支持的数组中:

if( in_array( $fileArray[\'type\'], $supportedTypes ) ) {

如果条件为true,您可以继续上载,否则文件不会上载,您可以退出。

SO网友:pallavi

在下面的代码中,我们找到了上传文件的mime类型。然后使用switch case检查上传文件的mime类型是否为application/pdf。

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES[\'file\'][\'tmp_name\']);
$ok = false;
switch ($mime) {
   case \'application/pdf\';
        $ok = true;
   default:
       die("Unknown/not permitted file type");
}

SO网友:Nikhil sHETH

lurie添加以下条件。。。

if ( $type == $imagetype ) {
// write code for reduce .jpg/.jpeg files size or call function here
}

结束