0

I use a function in my theme that prevents upload of big size image files (thanks to @fischi), but I want to apply it strictly to .jpg/.jpeg files only, because it prevents to upload .pdf files too. How to adapt this function?

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

This is the improved code (not tested yet), thanks to @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 Answers 3

2

I like to use the following:

First I initiate an array of supported mime types:

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

Next I get my actual uploaded file type:

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

Then I use wp_check_filetype() to get the extension and mimetype: $fileArray = wp_check_filetype( basename( $_FILES['name'][0] ) );

Finally I run a conditional to make sure that the uploaded mimetype is in my supported array:

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

IF the conditional is true you can proceed with your upload, otherwise the file won't upload and you can bail out.

-1

In below code we find out mime type of uploaded file. Then using switch case we check mime type of uploaded file is application/pdf or not.

$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");
}
2
  • 2
    Hi Pallavi, can you please edit your post to explain how this answers the question, and what the code does? Code only answers are discouraged. Thanks.
    – Tim Malone
    Commented Jul 20, 2016 at 7:05
  • 1
    Downvoted due to lack of context, seems like you do not want to learn or take advice from us. I will keep downvoting your answers as poor quality. I'm not going to comment on all your posts and ask you to add PROPER explanations to your code Commented Jul 20, 2016 at 7:13
-3

lurie add below condition...

if ( $type == $imagetype ) {
// write code for reduce .jpg/.jpeg files size or call function here
}
2
  • 1
    Please add an explanation to your code Commented Nov 10, 2014 at 11:00
  • @nikhil-sheth Thanks! I edited the question because of some errors and added an improved code with your suggestion. What you think, is this correct?
    – Yuri
    Commented Nov 10, 2014 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.