Site icon Hip-Hop Website Design and Development

connect pictures to publish

I’ve a type within the frontend to submit publish , I need to connect pictures uploaded through the shape to the publish.
that is my codes:

$post_sent = wp_insert_post(array(
    'post_title' => esc_sql($_POST['title']),
    'post_content' => wp_kses_post($_POST['content']),
    'post_author' => $user_id,
    'post_status' => 'pending',
    'post_type' => 'doc',
));                   
if ($_POST['_photo_id']) {
    foreach ($_POST['_photo_id'] as $file => $array) {
        $filename = get_attached_file( $file );
        $attachment = array(
            'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
           'post_mime_type' => $filetype['type'],
           'post_title'     => preg_replace( '/.[^.]+$/', '', basename( $filename ) ),
          'post_content'   => '',
          'post_status'    => 'inherit'
     );                                                        
     wp_insert_attachment($attachment,esc_sql($file['file']),$post_sent);         
}