media_handle_upload

The timeline below displays how wordpress function media_handle_upload has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Saves a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the `$_FILES` array that the file was sent.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Optional. Overwrite some of the attachment.
 * @param array  $overrides Optional. Override the wp_handle_upload() behavior.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    $post = get_post($post_id);
    if ($post) {
        // The post date doesn't usually matter for pages, so don't backdate this upload.
        if ('page' !== $post->post_type && substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name = $_FILES[$file_id]['name'];
    $ext = pathinfo($name, PATHINFO_EXTENSION);
    $name = wp_basename($name, ".{$ext}");
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: Audio track title, 2: Album title, 3: Artist name. */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: Audio track title, 2: Album title. */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: Audio track title, 2: Artist name. */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                /* translators: %s: Audio track title. */
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: Audio album title, 2: Artist name. */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            /* translators: Audio file track information. %d: Year of audio track release. */
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (is_numeric($track_number[0])) {
                if (isset($track_number[1]) && is_numeric($track_number[1])) {
                    $content .= ' ' . sprintf(
                        /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks. */
                        __('Track %1$s of %2$s.'),
                        number_format_i18n($track_number[0]),
                        number_format_i18n($track_number[1])
                    );
                } else {
                    $content .= ' ' . sprintf(
                        /* translators: Audio file track information. %s: Audio track number. */
                        __('Track %s.'),
                        number_format_i18n($track_number[0])
                    );
                }
            }
        }
        if (!empty($meta['genre'])) {
            /* translators: Audio file genre information. %s: Audio genre name. */
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (str_starts_with($type, 'image/')) {
        $image_meta = wp_read_image_metadata($file);
        if ($image_meta) {
            if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
                $title = $image_meta['title'];
            }
            if (trim($image_meta['caption'])) {
                $excerpt = $image_meta['caption'];
            }
        }
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data.
    $attachment_id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($attachment_id)) {
        /*
         * Set a custom header with the attachment_id.
         * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
         */
        if (!headers_sent()) {
            header('X-WP-Upload-Attachment-ID: ' . $attachment_id);
        }
        /*
         * The image sub-sizes are created during wp_generate_attachment_metadata().
         * This is generally slow and may cause timeouts or out of memory errors.
         */
        wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file));
    }
    return $attachment_id;
}

WordPress Version: 6.1

/**
 * Saves a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the `$_FILES` array that the file was sent.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Optional. Overwrite some of the attachment.
 * @param array  $overrides Optional. Override the wp_handle_upload() behavior.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    $post = get_post($post_id);
    if ($post) {
        // The post date doesn't usually matter for pages, so don't backdate this upload.
        if ('page' !== $post->post_type && substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name = $_FILES[$file_id]['name'];
    $ext = pathinfo($name, PATHINFO_EXTENSION);
    $name = wp_basename($name, ".{$ext}");
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: Audio track title, 2: Album title, 3: Artist name. */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: Audio track title, 2: Album title. */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: Audio track title, 2: Artist name. */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                /* translators: %s: Audio track title. */
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: Audio album title, 2: Artist name. */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            /* translators: Audio file track information. %d: Year of audio track release. */
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (is_numeric($track_number[0])) {
                if (isset($track_number[1]) && is_numeric($track_number[1])) {
                    $content .= ' ' . sprintf(
                        /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks. */
                        __('Track %1$s of %2$s.'),
                        number_format_i18n($track_number[0]),
                        number_format_i18n($track_number[1])
                    );
                } else {
                    $content .= ' ' . sprintf(
                        /* translators: Audio file track information. %s: Audio track number. */
                        __('Track %s.'),
                        number_format_i18n($track_number[0])
                    );
                }
            }
        }
        if (!empty($meta['genre'])) {
            /* translators: Audio file genre information. %s: Audio genre name. */
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/')) {
        $image_meta = wp_read_image_metadata($file);
        if ($image_meta) {
            if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
                $title = $image_meta['title'];
            }
            if (trim($image_meta['caption'])) {
                $excerpt = $image_meta['caption'];
            }
        }
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data.
    $attachment_id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($attachment_id)) {
        // Set a custom header with the attachment_id.
        // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
        if (!headers_sent()) {
            header('X-WP-Upload-Attachment-ID: ' . $attachment_id);
        }
        // The image sub-sizes are created during wp_generate_attachment_metadata().
        // This is generally slow and may cause timeouts or out of memory errors.
        wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file));
    }
    return $attachment_id;
}

WordPress Version: 5.9

/**
 * Saves a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the `$_FILES` array that the file was sent.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Optional. Overwrite some of the attachment.
 * @param array  $overrides Optional. Override the wp_handle_upload() behavior.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    $post = get_post($post_id);
    if ($post) {
        // The post date doesn't usually matter for pages, so don't backdate this upload.
        if ('page' !== $post->post_type && substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name = $_FILES[$file_id]['name'];
    $ext = pathinfo($name, PATHINFO_EXTENSION);
    $name = wp_basename($name, ".{$ext}");
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: Audio track title, 2: Album title, 3: Artist name. */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: Audio track title, 2: Album title. */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: Audio track title, 2: Artist name. */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                /* translators: %s: Audio track title. */
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: Audio album title, 2: Artist name. */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            /* translators: Audio file track information. %d: Year of audio track release. */
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks. */
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                /* translators: Audio file track information. %s: Audio track number. */
                $content .= ' ' . sprintf(__('Track %s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            /* translators: Audio file genre information. %s: Audio genre name. */
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/')) {
        $image_meta = wp_read_image_metadata($file);
        if ($image_meta) {
            if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
                $title = $image_meta['title'];
            }
            if (trim($image_meta['caption'])) {
                $excerpt = $image_meta['caption'];
            }
        }
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data.
    $attachment_id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($attachment_id)) {
        // Set a custom header with the attachment_id.
        // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
        if (!headers_sent()) {
            header('X-WP-Upload-Attachment-ID: ' . $attachment_id);
        }
        // The image sub-sizes are created during wp_generate_attachment_metadata().
        // This is generally slow and may cause timeouts or out of memory errors.
        wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file));
    }
    return $attachment_id;
}

WordPress Version: 5.5

/**
 * Saves a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the `$_FILES` array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Optional. Overwrite some of the attachment.
 * @param array  $overrides Optional. Override the wp_handle_upload() behavior.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    $post = get_post($post_id);
    if ($post) {
        // The post date doesn't usually matter for pages, so don't backdate this upload.
        if ('page' !== $post->post_type && substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name = $_FILES[$file_id]['name'];
    $ext = pathinfo($name, PATHINFO_EXTENSION);
    $name = wp_basename($name, ".{$ext}");
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: Audio track title, 2: Album title, 3: Artist name. */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: Audio track title, 2: Album title. */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: Audio track title, 2: Artist name. */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                /* translators: %s: Audio track title. */
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: Audio album title, 2: Artist name. */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            /* translators: Audio file track information. %d: Year of audio track release. */
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks. */
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                /* translators: Audio file track information. %s: Audio track number. */
                $content .= ' ' . sprintf(__('Track %s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            /* translators: Audio file genre information. %s: Audio genre name. */
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/')) {
        $image_meta = wp_read_image_metadata($file);
        if ($image_meta) {
            if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
                $title = $image_meta['title'];
            }
            if (trim($image_meta['caption'])) {
                $excerpt = $image_meta['caption'];
            }
        }
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data.
    $attachment_id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($attachment_id)) {
        // Set a custom header with the attachment_id.
        // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
        if (!headers_sent()) {
            header('X-WP-Upload-Attachment-ID: ' . $attachment_id);
        }
        // The image sub-sizes are created during wp_generate_attachment_metadata().
        // This is generally slow and may cause timeouts or out of memory errors.
        wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file));
    }
    return $attachment_id;
}

WordPress Version: 5.4

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the `$_FILES` array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the wp_handle_upload() behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    $post = get_post($post_id);
    if ($post) {
        // The post date doesn't usually matter for pages, so don't backdate this upload.
        if ('page' !== $post->post_type && substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name = $_FILES[$file_id]['name'];
    $ext = pathinfo($name, PATHINFO_EXTENSION);
    $name = wp_basename($name, ".{$ext}");
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: Audio track title, 2: Album title, 3: Artist name. */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: Audio track title, 2: Album title. */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: Audio track title, 2: Artist name. */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                /* translators: %s: Audio track title. */
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: Audio album title, 2: Artist name. */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            /* translators: Audio file track information. %d: Year of audio track release. */
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks. */
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                /* translators: Audio file track information. %s: Audio track number. */
                $content .= ' ' . sprintf(__('Track %s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            /* translators: Audio file genre information. %s: Audio genre name. */
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/')) {
        $image_meta = wp_read_image_metadata($file);
        if ($image_meta) {
            if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
                $title = $image_meta['title'];
            }
            if (trim($image_meta['caption'])) {
                $excerpt = $image_meta['caption'];
            }
        }
    }
    // Construct the attachment array.
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data.
    $attachment_id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($attachment_id)) {
        // Set a custom header with the attachment_id.
        // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
        if (!headers_sent()) {
            header('X-WP-Upload-Attachment-ID: ' . $attachment_id);
        }
        // The image sub-sizes are created during wp_generate_attachment_metadata().
        // This is generally slow and may cause timeouts or out of memory errors.
        wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file));
    }
    return $attachment_id;
}

WordPress Version: 5.3

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the `$_FILES` array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the wp_handle_upload() behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    $post = get_post($post_id);
    if ($post) {
        // The post date doesn't usually matter for pages, so don't backdate this upload.
        if ('page' !== $post->post_type && substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name = $_FILES[$file_id]['name'];
    $ext = pathinfo($name, PATHINFO_EXTENSION);
    $name = wp_basename($name, ".{$ext}");
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: Audio track title, 2: Album title, 3: Artist name. */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: Audio track title, 2: Album title. */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: Audio track title, 2: Artist name. */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                /* translators: %s: Audio track title. */
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: Audio album title, 2: Artist name. */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            /* translators: Audio file track information. %d: Year of audio track release. */
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks. */
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                /* translators: Audio file track information. %s: Audio track number. */
                $content .= ' ' . sprintf(__('Track %s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            /* translators: Audio file genre information. %s: Audio genre name. */
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/')) {
        $image_meta = wp_read_image_metadata($file);
        if ($image_meta) {
            if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
                $title = $image_meta['title'];
            }
            if (trim($image_meta['caption'])) {
                $excerpt = $image_meta['caption'];
            }
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $attachment_id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($attachment_id)) {
        // Set a custom header with the attachment_id.
        // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
        if (!headers_sent()) {
            header('X-WP-Upload-Attachment-ID: ' . $attachment_id);
        }
        // The image sub-sizes are created during wp_generate_attachment_metadata().
        // This is generally slow and may cause timeouts or out of memory errors.
        wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file));
    }
    return $attachment_id;
}

WordPress Version: 5.1

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the `$_FILES` array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the wp_handle_upload() behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        // The post date doesn't usually matter for pages, so don't backdate this upload.
        if ('page' !== $post->post_type && substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name = $_FILES[$file_id]['name'];
    $ext = pathinfo($name, PATHINFO_EXTENSION);
    $name = wp_basename($name, ".{$ext}");
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                /* translators: 1: audio track title */
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            /* translators: Audio file track information. %d: Year of audio track release */
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks */
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                /* translators: Audio file track information. %s: Audio track number */
                $content .= ' ' . sprintf(__('Track %s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            /* translators: Audio file genre information. %s: Audio genre name */
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .10

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the `$_FILES` array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the wp_handle_upload() behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        // The post date doesn't usually matter for pages, so don't backdate this upload.
        if ('page' !== $post->post_type && substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name = $_FILES[$file_id]['name'];
    $ext = pathinfo($name, PATHINFO_EXTENSION);
    $name = wp_basename($name, ".{$ext}");
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                /* translators: 1: audio track title */
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            /* translators: Audio file track information. 1: Year of audio track release */
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks */
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                /* translators: Audio file track information. 1: Audio track number */
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            /* translators: Audio file genre information. 1: Audio genre name */
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.9

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the `$_FILES` array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the wp_handle_upload() behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        // The post date doesn't usually matter for pages, so don't backdate this upload.
        if ('page' !== $post->post_type && substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name = $_FILES[$file_id]['name'];
    $ext = pathinfo($name, PATHINFO_EXTENSION);
    $name = wp_basename($name, ".{$ext}");
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                /* translators: 1: audio track title */
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            /* translators: Audio file track information. 1: Year of audio track release */
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks */
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                /* translators: Audio file track information. 1: Audio track number */
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            /* translators: Audio file genre information. 1: Audio genre name */
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id, true);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.7

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the `$_FILES` array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the wp_handle_upload() behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name = $_FILES[$file_id]['name'];
    $ext = pathinfo($name, PATHINFO_EXTENSION);
    $name = wp_basename($name, ".{$ext}");
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                /* translators: 1: audio track title */
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            /* translators: Audio file track information. 1: Year of audio track release */
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks */
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                /* translators: Audio file track information. 1: Audio track number */
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            /* translators: Audio file genre information. 1: Audio genre name */
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .10

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the `$_FILES` array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the wp_handle_upload() behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 6.1

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the `$_FILES` array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the wp_handle_upload() behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_title($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.6

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the `$_FILES` array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the wp_handle_upload() behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 5.5

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 5.4

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_title($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .30

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 5.3

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .20

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 5.2

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .10

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.5

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.6

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.5

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_title($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.4

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .30

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.3

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .20

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.2

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .10

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.4

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 3.7

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 3.6

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_title($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 3.4

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .30

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 3.3

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .20

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 3.2

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .10

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.3

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    unset($attachment['ID']);
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 2.4

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .30

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 2.3

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .20

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 2.2

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .11

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .10

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_title($name);
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.2

/**
 * Save a file submitted from a POST request and create an attachment post for it.
 *
 * @since 2.5.0
 *
 * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 *                          be set to 0, creating a media item that has no relationship to a post.
 * @param array  $post_data Overwrite some of the attachment. Optional.
 * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    $excerpt = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } elseif (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } elseif (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } elseif (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } elseif (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $excerpt = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 1.5

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .40

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 1.4

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .30

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 1.3

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .20

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 1.2

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .14

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .13

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_title($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.1

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif (0 === strpos($type, 'image/') && $image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 0.4

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .30

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 0.3

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .20

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 0.2

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .14

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .13

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_title($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 4.0

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // Use image exif/iptc data for title and caption defaults if possible.
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 9.2

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .15

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .14

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_title($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 3.9

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 8.4

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .30

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 8.3

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .20

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 8.2

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .17

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .16

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_title($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 7.5

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .40

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 7.4

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .30

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 7.3

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .20

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 7.2

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .17

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_text_field($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: .16

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = sanitize_title($name);
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}

WordPress Version: 3.7

/**
 * This handles the file upload POST itself, creating the attachment post.
 *
 * @since 2.5.0
 *
 * @param string $file_id Index into the {@link $_FILES} array of the upload
 * @param int $post_id The post ID the media is associated with
 * @param array $post_data allows you to overwrite some of the attachment
 * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 * @return int the ID of the attachment
 */
function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false))
{
    $time = current_time('mysql');
    if ($post = get_post($post_id)) {
        if (substr($post->post_date, 0, 4) > 0) {
            $time = $post->post_date;
        }
    }
    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $name_parts = pathinfo($name);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = $name;
    $content = '';
    if (preg_match('#^audio#', $type)) {
        $meta = wp_read_audio_metadata($file);
        if (!empty($meta['title'])) {
            $title = $meta['title'];
        }
        $content = '';
        if (!empty($title)) {
            if (!empty($meta['album']) && !empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: album title, 3: artist name */
                $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']);
            } else if (!empty($meta['album'])) {
                /* translators: 1: audio track title, 2: album title */
                $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']);
            } else if (!empty($meta['artist'])) {
                /* translators: 1: audio track title, 2: artist name */
                $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']);
            } else {
                $content .= sprintf(__('"%s".'), $title);
            }
        } else if (!empty($meta['album'])) {
            if (!empty($meta['artist'])) {
                /* translators: 1: audio album title, 2: artist name */
                $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']);
            } else {
                $content .= $meta['album'] . '.';
            }
        } else if (!empty($meta['artist'])) {
            $content .= $meta['artist'] . '.';
        }
        if (!empty($meta['year'])) {
            $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']);
        }
        if (!empty($meta['track_number'])) {
            $track_number = explode('/', $meta['track_number']);
            if (isset($track_number[1])) {
                $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1]));
            } else {
                $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0]));
            }
        }
        if (!empty($meta['genre'])) {
            $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']);
        }
        // use image exif/iptc data for title and caption defaults if possible
    } elseif ($image_meta = @wp_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);
    if (!is_wp_error($id)) {
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
    }
    return $id;
}