how to upload a image from frontend with wp_insert_post and also update_post_meta?

I am using cmb2 for frontend form submission with custom post type name 'form'. But can not upload image with that. the first step is:

    array(
            'name' = 'Image',
            'type' = 'file',
            'id' = '_first_image',
            'options' = array(
                'url' = true, // Hide the text input for the url
            ),
            'text'    = array(
                'add_upload_file_text' = 'Upload your image image', // Change upload button text. Default: Add or Upload File
            ),
        ),

my form is:

 form method=post action= id=form
        div class=student-image
            label for=student_imageImage/label
            input name=image type=file id=img accept=image/png, image/jpeg, image/jpg
        /div

 input style=color: white; name=submit type=submit value=Submit
 /form

PHP code:

if(isset($_POST['submit'])){

$args = array(
    'post_status' = 'draft',
    'post_type' = 'form',
    'post_author' = $user_ID,
    'post_title' = $_POST['name'],
    'post_content' = $_POST['subject'],
    
);
$query = wp_insert_post( $args, false );
$image = $_POST['image'];
if(query){
         update_post_meta( $query, '_first_image', $image );
  }
}

This is my code. I can't upload any image. what can I do for uploading images and what code do I add to complete. and also add to upload only one image. can any one give me the solution. I am trying other solution but those can't work.

Topic input wp-insert-post wp-load.php attachments custom-field Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.