如果上传的文件不是图像/gif,如何停止发布帖子

时间:2016-03-11 作者:I.Alex

这是代码,如果文件不是图像,我想停止后期发布。这可能吗?

case "window-image":
        $format = \'image\';
        $title = esc_html($_POST[\'title_image\']);
        $tags = $_POST[\'tags_image\'];
        $comments = isset($_POST[\'comments_image\']) ? 1 : 0;
        $anonymous = isset($_POST[\'anonymous_image\']) ? 1 : 0;
        $comment_status = $comments == 1 ? \'open\' : \'closed\';
        $user_id = $id;

        if(($_FILES[\'image_imagepost\'][\'error\'] != 0 && $image == \'\') || $title == \'\') {
            wp_redirect(home_url(\'/\') . \'?posterror=1\');
            exit; 
        }       

        $post = array(
          \'comment_status\' => $comment_status,
          \'ping_status\'    => \'open\',
          \'post_author\'    => $user_id,
          \'post_content\'   => \'\',
          \'post_status\'    => $status,
          \'post_title\'     => $title,
          \'post_type\'      => \'post\',
         );  
        $post_id = wp_insert_post($post);

#Create attachment
    if (!function_exists(\'wp_generate_attachment_metadata\')){
        require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
        require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
        require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
    }
        $upload = wp_upload_bits( $_FILES[\'file\'][\'name\'], null, file_get_contents( $_FILES[\'file\'][\'tmp_name\'] ) );
        $wp_filetype = wp_check_filetype( basename( $upload[\'file\'] ), null );
        $wp_upload_dir = wp_upload_dir();
        $attachment = array(
            \'guid\' => $wp_upload_dir[\'baseurl\'] . _wp_relative_upload_path( $upload[\'file\'] ),
            \'post_mime_type\' => $wp_filetype[\'type\'],
            \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename( $upload[\'file\'] )),    
            \'post_content\' => \'\',
            \'post_author\' => $user_id,
            \'post_status\' => \'inherit\'
        );
        $attach_id = wp_insert_attachment( $attachment, $upload[\'file\'], $post_id );
        require_once(ABSPATH . \'wp-admin/includes/image.php\');
        $attach_data = wp_generate_attachment_metadata( $attach_id, $upload[\'file\'] );
        wp_update_attachment_metadata( $attach_id, $attach_data );
#Create attachment

#Use attachment
        update_post_meta( $post_id, \'_thumbnail_id\', $attach_id );
#Use attachment


        $nsfw = isset($_POST[\'nsfw_image\']) ? 1 : 2;
        add_post_meta($post_id, \'_standard_nsfw\', $nsfw);
        add_post_meta($post_id, \'_anonymous\', $anonymous);

        break;

1 个回复
SO网友:I.Alex

看来我成功了。我把这个代码放进了上传代码

$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  );
}
这允许用户只发布图像,如果他上传另一种文件类型,就会收到一条消息,帖子就会自动被丢弃。

相关推荐

Wordpress cutting images size

我目前正在制作一个页面的横幅,过了一段时间,我注意到我的图像质量比我预期的稍差一些。我上传了1920x1024大小的图片,但在我查看管理仪表板后,我注意到它们已经缩小到1024x575!这有什么原因吗?有什么办法可以防止这种情况发生吗?提前感谢!