Retrieve the URL for an attachment.

Signature

wp_get_attachment_url( $post_id = 0 )
post_id
 (int) Optional. Attachment ID. Default 0.
Default: 0

Return

(string|boolean) Attachment URL, otherwise false.

Source

function wp_get_attachment_url( $post_id = 0 ) {
	$post_id = (int) $post_id;
	if ( !$post = get_post( $post_id ) )
		return false;

	if ( 'attachment' != $post->post_type )
		return false;

	$url = '';
	// Get attached file.
	if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) {
		// Get upload directory.
		if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
			// Check that the upload base exists in the file location.
			if ( 0 === strpos( $file, $uploads['basedir'] ) ) {
				// Replace file location with url location.
				$url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
			} elseif ( false !== strpos($file, 'wp-content/uploads') ) {
				$url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 );
			} else {
29 more lines...
WP Trac GitHub

Link here