wp_upload_bits

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

WordPress Version: 6.3

/**
 * Creates a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string      $name       Filename.
 * @param null|string $deprecated Never used. Set to null.
 * @param string      $bits       File content
 * @param string      $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array {
 *     Information about the newly-uploaded file.
 *
 *     @type string       $file  Filename of the newly-uploaded file.
 *     @type string       $url   URL of the uploaded file.
 *     @type string       $type  File type.
 *     @type string|false $error Error message, if there has been an error.
 * }
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Sorry, you are not allowed to upload this file type.'));
    }
    $upload = wp_upload_dir($time);
    if (false !== $upload['error']) {
        return $upload;
    }
    /**
     * Filters whether to treat the upload bits as an error.
     *
     * Returning a non-array from the filter will effectively short-circuit preparing the upload bits
     * and return that value instead. An error message should be returned as a string.
     *
     * @since 3.0.0
     *
     * @param array|string $upload_bits_error An array of upload bits data, or error message to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (str_starts_with($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = wp_basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(
            /* translators: %s: Directory path. */
            __('Unable to create directory %s. Is its parent directory writable by the server?'),
            $error_path
        );
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array(
            /* translators: %s: File name. */
            'error' => sprintf(__('Could not write file %s'), $new_file),
        );
    }
    fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions.
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL.
    $url = $upload['url'] . "/{$filename}";
    if (is_multisite()) {
        clean_dirsize_cache($new_file);
    }
    /** This filter is documented in wp-admin/includes/file.php */
    return apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false), 'sideload');
}

WordPress Version: 6.1

/**
 * Creates a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string      $name       Filename.
 * @param null|string $deprecated Never used. Set to null.
 * @param string      $bits       File content
 * @param string      $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array {
 *     Information about the newly-uploaded file.
 *
 *     @type string       $file  Filename of the newly-uploaded file.
 *     @type string       $url   URL of the uploaded file.
 *     @type string       $type  File type.
 *     @type string|false $error Error message, if there has been an error.
 * }
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Sorry, you are not allowed to upload this file type.'));
    }
    $upload = wp_upload_dir($time);
    if (false !== $upload['error']) {
        return $upload;
    }
    /**
     * Filters whether to treat the upload bits as an error.
     *
     * Returning a non-array from the filter will effectively short-circuit preparing the upload bits
     * and return that value instead. An error message should be returned as a string.
     *
     * @since 3.0.0
     *
     * @param array|string $upload_bits_error An array of upload bits data, or error message to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = wp_basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(
            /* translators: %s: Directory path. */
            __('Unable to create directory %s. Is its parent directory writable by the server?'),
            $error_path
        );
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array(
            /* translators: %s: File name. */
            'error' => sprintf(__('Could not write file %s'), $new_file),
        );
    }
    fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions.
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL.
    $url = $upload['url'] . "/{$filename}";
    if (is_multisite()) {
        clean_dirsize_cache($new_file);
    }
    /** This filter is documented in wp-admin/includes/file.php */
    return apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false), 'sideload');
}

WordPress Version: 5.9

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string      $name       Filename.
 * @param null|string $deprecated Never used. Set to null.
 * @param string      $bits       File content
 * @param string      $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array {
 *     Information about the newly-uploaded file.
 *
 *     @type string       $file  Filename of the newly-uploaded file.
 *     @type string       $url   URL of the uploaded file.
 *     @type string       $type  File type.
 *     @type string|false $error Error message, if there has been an error.
 * }
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Sorry, you are not allowed to upload this file type.'));
    }
    $upload = wp_upload_dir($time);
    if (false !== $upload['error']) {
        return $upload;
    }
    /**
     * Filters whether to treat the upload bits as an error.
     *
     * Returning a non-array from the filter will effectively short-circuit preparing the upload bits
     * and return that value instead. An error message should be returned as a string.
     *
     * @since 3.0.0
     *
     * @param array|string $upload_bits_error An array of upload bits data, or error message to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = wp_basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(
            /* translators: %s: Directory path. */
            __('Unable to create directory %s. Is its parent directory writable by the server?'),
            $error_path
        );
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array(
            /* translators: %s: File name. */
            'error' => sprintf(__('Could not write file %s'), $new_file),
        );
    }
    fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions.
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL.
    $url = $upload['url'] . "/{$filename}";
    if (is_multisite()) {
        clean_dirsize_cache($new_file);
    }
    /** This filter is documented in wp-admin/includes/file.php */
    return apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false), 'sideload');
}

WordPress Version: 5.6

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string      $name       Filename.
 * @param null|string $deprecated Never used. Set to null.
 * @param string      $bits       File content
 * @param string      $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array {
 *     Information about the newly-uploaded file.
 *
 *     @type string       $file  Filename of the newly-uploaded file.
 *     @type string       $url   URL of the uploaded file.
 *     @type string       $type  File type.
 *     @type string|false $error Error message, if there has been an error.
 * }
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Sorry, this file type is not permitted for security reasons.'));
    }
    $upload = wp_upload_dir($time);
    if (false !== $upload['error']) {
        return $upload;
    }
    /**
     * Filters whether to treat the upload bits as an error.
     *
     * Returning a non-array from the filter will effectively short-circuit preparing the upload bits
     * and return that value instead. An error message should be returned as a string.
     *
     * @since 3.0.0
     *
     * @param array|string $upload_bits_error An array of upload bits data, or error message to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = wp_basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(
            /* translators: %s: Directory path. */
            __('Unable to create directory %s. Is its parent directory writable by the server?'),
            $error_path
        );
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array(
            /* translators: %s: File name. */
            'error' => sprintf(__('Could not write file %s'), $new_file),
        );
    }
    fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions.
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL.
    $url = $upload['url'] . "/{$filename}";
    if (is_multisite()) {
        clean_dirsize_cache($new_file);
    }
    /** This filter is documented in wp-admin/includes/file.php */
    return apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false), 'sideload');
}

WordPress Version: 5.5

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string      $name       Filename.
 * @param null|string $deprecated Never used. Set to null.
 * @param string      $bits       File content
 * @param string      $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array {
 *     Information about the newly-uploaded file.
 *
 *     @type string       $file  Filename of the newly-uploaded file.
 *     @type string       $url   URL of the uploaded file.
 *     @type string       $type  File type.
 *     @type string|false $error Error message, if there has been an error.
 * }
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Sorry, this file type is not permitted for security reasons.'));
    }
    $upload = wp_upload_dir($time);
    if (false !== $upload['error']) {
        return $upload;
    }
    /**
     * Filters whether to treat the upload bits as an error.
     *
     * Returning a non-array from the filter will effectively short-circuit preparing the upload bits
     * and return that value instead. An error message should be returned as a string.
     *
     * @since 3.0.0
     *
     * @param array|string $upload_bits_error An array of upload bits data, or error message to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = wp_basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(
            /* translators: %s: Directory path. */
            __('Unable to create directory %s. Is its parent directory writable by the server?'),
            $error_path
        );
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array(
            /* translators: %s: File name. */
            'error' => sprintf(__('Could not write file %s'), $new_file),
        );
    }
    fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions.
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL.
    $url = $upload['url'] . "/{$filename}";
    /** This filter is documented in wp-admin/includes/file.php */
    return apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false), 'sideload');
}

WordPress Version: 5.4

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string       $name       Filename.
 * @param null|string  $deprecated Never used. Set to null.
 * @param string       $bits       File content
 * @param string       $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Sorry, this file type is not permitted for security reasons.'));
    }
    $upload = wp_upload_dir($time);
    if (false !== $upload['error']) {
        return $upload;
    }
    /**
     * Filters whether to treat the upload bits as an error.
     *
     * Returning a non-array from the filter will effectively short-circuit preparing the upload
     * bits, returning that value instead. An error message should be returned as a string.
     *
     * @since 3.0.0
     *
     * @param array|string $upload_bits_error An array of upload bits data, or error message to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = wp_basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(
            /* translators: %s: Directory path. */
            __('Unable to create directory %s. Is its parent directory writable by the server?'),
            $error_path
        );
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array(
            /* translators: %s: File name. */
            'error' => sprintf(__('Could not write file %s'), $new_file),
        );
    }
    fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions.
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL.
    $url = $upload['url'] . "/{$filename}";
    /** This filter is documented in wp-admin/includes/file.php */
    return apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false), 'sideload');
}

WordPress Version: 5.3

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string       $name       Filename.
 * @param null|string  $deprecated Never used. Set to null.
 * @param mixed        $bits       File content
 * @param string       $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Sorry, this file type is not permitted for security reasons.'));
    }
    $upload = wp_upload_dir($time);
    if ($upload['error'] !== false) {
        return $upload;
    }
    /**
     * Filters whether to treat the upload bits as an error.
     *
     * Passing a non-array to the filter will effectively short-circuit preparing
     * the upload bits, returning that value instead.
     *
     * @since 3.0.0
     *
     * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = wp_basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(
            /* translators: %s: Directory path. */
            __('Unable to create directory %s. Is its parent directory writable by the server?'),
            $error_path
        );
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array(
            /* translators: %s: File name. */
            'error' => sprintf(__('Could not write file %s'), $new_file),
        );
    }
    fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL
    $url = $upload['url'] . "/{$filename}";
    /** This filter is documented in wp-admin/includes/file.php */
    return apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false), 'sideload');
}

WordPress Version: 5.2

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string       $name       Filename.
 * @param null|string  $deprecated Never used. Set to null.
 * @param mixed        $bits       File content
 * @param string       $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Sorry, this file type is not permitted for security reasons.'));
    }
    $upload = wp_upload_dir($time);
    if ($upload['error'] !== false) {
        return $upload;
    }
    /**
     * Filters whether to treat the upload bits as an error.
     *
     * Passing a non-array to the filter will effectively short-circuit preparing
     * the upload bits, returning that value instead.
     *
     * @since 3.0.0
     *
     * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = wp_basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(
            /* translators: %s: directory path */
            __('Unable to create directory %s. Is its parent directory writable by the server?'),
            $error_path
        );
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array('error' => sprintf(__('Could not write file %s'), $new_file));
    }
    @fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    @chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL
    $url = $upload['url'] . "/{$filename}";
    /** This filter is documented in wp-admin/includes/file.php */
    return apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false), 'sideload');
}

WordPress Version: 4.8

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string       $name       Filename.
 * @param null|string  $deprecated Never used. Set to null.
 * @param mixed        $bits       File content
 * @param string       $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Sorry, this file type is not permitted for security reasons.'));
    }
    $upload = wp_upload_dir($time);
    if ($upload['error'] !== false) {
        return $upload;
    }
    /**
     * Filters whether to treat the upload bits as an error.
     *
     * Passing a non-array to the filter will effectively short-circuit preparing
     * the upload bits, returning that value instead.
     *
     * @since 3.0.0
     *
     * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(
            /* translators: %s: directory path */
            __('Unable to create directory %s. Is its parent directory writable by the server?'),
            $error_path
        );
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array('error' => sprintf(__('Could not write file %s'), $new_file));
    }
    @fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    @chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL
    $url = $upload['url'] . "/{$filename}";
    /** This filter is documented in wp-admin/includes/file.php */
    return apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false), 'sideload');
}

WordPress Version: 4.7

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string       $name       Filename.
 * @param null|string  $deprecated Never used. Set to null.
 * @param mixed        $bits       File content
 * @param string       $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Invalid file type'));
    }
    $upload = wp_upload_dir($time);
    if ($upload['error'] !== false) {
        return $upload;
    }
    /**
     * Filters whether to treat the upload bits as an error.
     *
     * Passing a non-array to the filter will effectively short-circuit preparing
     * the upload bits, returning that value instead.
     *
     * @since 3.0.0
     *
     * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(
            /* translators: %s: directory path */
            __('Unable to create directory %s. Is its parent directory writable by the server?'),
            $error_path
        );
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array('error' => sprintf(__('Could not write file %s'), $new_file));
    }
    @fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    @chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL
    $url = $upload['url'] . "/{$filename}";
    /** This filter is documented in wp-admin/includes/file.php */
    return apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false), 'sideload');
}

WordPress Version: 4.6

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string       $name       Filename.
 * @param null|string  $deprecated Never used. Set to null.
 * @param mixed        $bits       File content
 * @param string       $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Invalid file type'));
    }
    $upload = wp_upload_dir($time);
    if ($upload['error'] !== false) {
        return $upload;
    }
    /**
     * Filters whether to treat the upload bits as an error.
     *
     * Passing a non-array to the filter will effectively short-circuit preparing
     * the upload bits, returning that value instead.
     *
     * @since 3.0.0
     *
     * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $error_path);
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array('error' => sprintf(__('Could not write file %s'), $new_file));
    }
    @fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    @chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL
    $url = $upload['url'] . "/{$filename}";
    /** This filter is documented in wp-admin/includes/file.php */
    return apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false), 'sideload');
}

WordPress Version: 4.4

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string       $name       Filename.
 * @param null|string  $deprecated Never used. Set to null.
 * @param mixed        $bits       File content
 * @param string       $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Invalid file type'));
    }
    $upload = wp_upload_dir($time);
    if ($upload['error'] !== false) {
        return $upload;
    }
    /**
     * Filter whether to treat the upload bits as an error.
     *
     * Passing a non-array to the filter will effectively short-circuit preparing
     * the upload bits, returning that value instead.
     *
     * @since 3.0.0
     *
     * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $error_path);
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array('error' => sprintf(__('Could not write file %s'), $new_file));
    }
    @fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    @chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL
    $url = $upload['url'] . "/{$filename}";
    /** This filter is documented in wp-admin/includes/file.php */
    return apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $wp_filetype['type'], 'error' => false), 'sideload');
}

WordPress Version: 4.1

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string       $name       Filename.
 * @param null|string  $deprecated Never used. Set to null.
 * @param mixed        $bits       File content
 * @param string       $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Invalid file type'));
    }
    $upload = wp_upload_dir($time);
    if ($upload['error'] !== false) {
        return $upload;
    }
    /**
     * Filter whether to treat the upload bits as an error.
     *
     * Passing a non-array to the filter will effectively short-circuit preparing
     * the upload bits, returning that value instead.
     *
     * @since 3.0.0
     *
     * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $error_path);
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array('error' => sprintf(__('Could not write file %s'), $new_file));
    }
    @fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    @chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL
    $url = $upload['url'] . "/{$filename}";
    return array('file' => $new_file, 'url' => $url, 'error' => false);
}

WordPress Version: 4.0

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string $name       Filename.
 * @param null   $deprecated Never used. Set to null.
 * @param mixed  $bits       File content
 * @param string $time       Optional. Time formatted in 'yyyy/mm'. Default null.
 * @return array
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Invalid file type'));
    }
    $upload = wp_upload_dir($time);
    if ($upload['error'] !== false) {
        return $upload;
    }
    /**
     * Filter whether to treat the upload bits as an error.
     *
     * Passing a non-array to the filter will effectively short-circuit preparing
     * the upload bits, returning that value instead.
     *
     * @since 3.0.0
     *
     * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $error_path);
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array('error' => sprintf(__('Could not write file %s'), $new_file));
    }
    @fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    @chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL
    $url = $upload['url'] . "/{$filename}";
    return array('file' => $new_file, 'url' => $url, 'error' => false);
}

WordPress Version: 3.9

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string $name
 * @param null $deprecated Never used. Set to null.
 * @param mixed $bits File content
 * @param string $time Optional. Time formatted in 'yyyy/mm'.
 * @return array
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Invalid file type'));
    }
    $upload = wp_upload_dir($time);
    if ($upload['error'] !== false) {
        return $upload;
    }
    /**
     * Filter whether to treat the upload bits as an error.
     *
     * Passing a non-array to the filter will effectively short-circuit preparing
     * the upload bits, returning that value instead.
     *
     * @since 3.0.0
     *
     * @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return.
     */
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $error_path);
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array('error' => sprintf(__('Could not write file %s'), $new_file));
    }
    @fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    @chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL
    $url = $upload['url'] . "/{$filename}";
    return array('file' => $new_file, 'url' => $url, 'error' => false);
}

WordPress Version: 3.7

/**
 * Create a file in the upload folder with given content.
 *
 * If there is an error, then the key 'error' will exist with the error message.
 * If success, then the key 'file' will have the unique file path, the 'url' key
 * will have the link to the new file. and the 'error' key will be set to false.
 *
 * This function will not move an uploaded file to the upload folder. It will
 * create a new file with the content in $bits parameter. If you move the upload
 * file, read the content of the uploaded file, and then you can give the
 * filename and content to this function, which will add it to the upload
 * folder.
 *
 * The permissions will be set on the new file automatically by this function.
 *
 * @since 2.0.0
 *
 * @param string $name
 * @param null $deprecated Never used. Set to null.
 * @param mixed $bits File content
 * @param string $time Optional. Time formatted in 'yyyy/mm'.
 * @return array
 */
function wp_upload_bits($name, $deprecated, $bits, $time = null)
{
    if (!empty($deprecated)) {
        _deprecated_argument(__FUNCTION__, '2.0');
    }
    if (empty($name)) {
        return array('error' => __('Empty filename'));
    }
    $wp_filetype = wp_check_filetype($name);
    if (!$wp_filetype['ext'] && !current_user_can('unfiltered_upload')) {
        return array('error' => __('Invalid file type'));
    }
    $upload = wp_upload_dir($time);
    if ($upload['error'] !== false) {
        return $upload;
    }
    $upload_bits_error = apply_filters('wp_upload_bits', array('name' => $name, 'bits' => $bits, 'time' => $time));
    if (!is_array($upload_bits_error)) {
        $upload['error'] = $upload_bits_error;
        return $upload;
    }
    $filename = wp_unique_filename($upload['path'], $name);
    $new_file = $upload['path'] . "/{$filename}";
    if (!wp_mkdir_p(dirname($new_file))) {
        if (0 === strpos($upload['basedir'], ABSPATH)) {
            $error_path = str_replace(ABSPATH, '', $upload['basedir']) . $upload['subdir'];
        } else {
            $error_path = basename($upload['basedir']) . $upload['subdir'];
        }
        $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?'), $error_path);
        return array('error' => $message);
    }
    $ifp = @fopen($new_file, 'wb');
    if (!$ifp) {
        return array('error' => sprintf(__('Could not write file %s'), $new_file));
    }
    @fwrite($ifp, $bits);
    fclose($ifp);
    clearstatcache();
    // Set correct file permissions
    $stat = @stat(dirname($new_file));
    $perms = $stat['mode'] & 07777;
    $perms = $perms & 0666;
    @chmod($new_file, $perms);
    clearstatcache();
    // Compute the URL
    $url = $upload['url'] . "/{$filename}";
    return array('file' => $new_file, 'url' => $url, 'error' => false);
}