I have the same issue just now, when I attach images to post programmatically, they appear as attached but in media, they show as broken images with duplicated "sites" path. I haven't found solution yet, did you?. This is the code for inserting attachment, the images insert correct, GUID for attachment shows correct path too.
//$filename is absolute image path , correct one.
$attachment = array(
'guid' => $filename ,
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id);
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );