Frontend Post - Allow Only Image File Upload

How I can allow user to upload only IMAGE file ?function.php Insert Attachmentfunction insert_attachment($file_handle

How I can allow user to upload only IMAGE file ?

function.php

// Insert Attachment
function insert_attachment($file_handler, $post_id, $setthumb='false') {
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __return_false(); 
} 
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');

//echo $attach_id = media_handle_upload($file_handler, $post_id);
$attach_id = media_handle_upload($file_handler, $post_id);

if ($setthumb == 1) update_post_meta($post_id, '_thumbnail_id', $attach_id);
    return $attach_id;
}

template-post.php

//Featured Image
if ($_FILES) {
    array_reverse($_FILES);
    $i = 0; //this will count the posts
    foreach ($_FILES as $file => $array) {
        if ($i == 0) $set_feature = 1; //if $i ==0 then we are dealing with the first post
        else $set_feature = 0; //if $i!=0 we are not dealing with the first post
        $newupload = insert_attachment($file, $post_id, $set_feature);
        //echo $i++;
        }
}

Need your help to complete this.

How I can allow user to upload only IMAGE file ?

function.php

// Insert Attachment
function insert_attachment($file_handler, $post_id, $setthumb='false') {
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __return_false(); 
} 
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');

//echo $attach_id = media_handle_upload($file_handler, $post_id);
$attach_id = media_handle_upload($file_handler, $post_id);

if ($setthumb == 1) update_post_meta($post_id, '_thumbnail_id', $attach_id);
    return $attach_id;
}

template-post.php

//Featured Image
if ($_FILES) {
    array_reverse($_FILES);
    $i = 0; //this will count the posts
    foreach ($_FILES as $file => $array) {
        if ($i == 0) $set_feature = 1; //if $i ==0 then we are dealing with the first post
        else $set_feature = 0; //if $i!=0 we are not dealing with the first post
        $newupload = insert_attachment($file, $post_id, $set_feature);
        //echo $i++;
        }
}

Need your help to complete this.

Share Improve this question asked Dec 25, 2019 at 22:11 memsmems 7312 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

You can check mime type of uploaded image before upload to media. Add mimeTypes in $allowmimeType which you want to allow. then check uploaded files mimetype $fileMimeType. If not found in allowed mimetype then return false.

// Insert Attachment
function insert_attachment($file_handler, $post_id, $setthumb='false') {
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __return_false(); 
    } 

    # uploaded file type
    $fileMimeType = $_FILES[$file_handler]['type'];

    # allowed types
    $allowmimeType = array(
        'png' => 'image/png',
        'jpeg' => 'image/jpeg',
        'gif' => 'image/gif',
    );

    # check if mime type type
     if(!in_array($fileMimeType,$allowmimeType) ){
       return __return_false(); 
    }

    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');

    //echo $attach_id = media_handle_upload($file_handler, $post_id);
    $attach_id = media_handle_upload($file_handler, $post_id);

    if ($setthumb == 1) update_post_meta($post_id, '_thumbnail_id', $attach_id);
        return $attach_id;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744879534a4598753.html

相关推荐

  • Frontend Post - Allow Only Image File Upload

    How I can allow user to upload only IMAGE file ?function.php Insert Attachmentfunction insert_attachment($file_handle

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信