How to stop a post from publishing if the file upload is not a image/gif

Seems like I managed to work .
I put this code into upload code

$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
    wp_trash_post( $post_id  );
}

And this allow users to post just images and if he upload another file type get a message and the post is automaticaly going to trash.