a tricky question about featured image

After

add_post_meta($post_id, '_standard_image', trailingslashit($uploaddir['url']) . $filename);

paste

set_post_thumbnail($post_id, $attach_id);

What this is doing is taking the attachment that is created in this function and adding it as a featured image.

Hope that helps.