$file_array['name'
] =
wp_basename( $matches[0
] );
// Download file to temp location.
$file_array['tmp_name'
] =
download_url( $file );
// If error storing temporarily, return the error.
if ( is_wp_error( $file_array['tmp_name'
] ) ) { return $file_array['tmp_name'
];
} // Do the validation and storage stuff.
$id =
media_handle_sideload( $file_array,
$post_id,
$desc );
// If error storing permanently, unlink.
if ( is_wp_error( $id ) ) { @
unlink( $file_array['tmp_name'
] );
return $id;
} // Store the original attachment source in meta.
add_post_meta( $id, '_source_url',
$file );
// If attachment ID was requested, return it.