i have following code:
$wp_filetype = wp_check_filetype( basename( $image_url ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url']. '/' . basename( $image_url ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/.[^.]+$/', '', basename($image_url)),
'post_content' => '',
'post_status' => 'publish'
);
$attach_id = wp_insert_attachment( $attachment, $image_url );
everything works fine, but the attached img tag has a different url than expected in its src attribute. ( wp-contents/uploads instead of wp-contents/uploads/2013/03/ )
Try changing
$wp_upload_dir['url']
to$wp_upload_dir['path']
, or$uploads['baseurl'] . $uploads['subdir']
.