unzip_file

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

WordPress Version: 5.5

/**
 * Unzips a specified ZIP file to a location on the filesystem via the WordPress
 * Filesystem Abstraction.
 *
 * Assumes that WP_Filesystem() has already been called and set up. Does not extract
 * a root-level __MACOSX directory, if present.
 *
 * Attempts to increase the PHP memory limit to 256M before uncompressing. However,
 * the most memory required shouldn't be much larger than the archive itself.
 *
 * @since 2.5.0
 *
 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 *
 * @param string $file Full path and filename of ZIP archive.
 * @param string $to   Full path on the filesystem to extract archive to.
 * @return true|WP_Error True on success, WP_Error on failure.
 */
function unzip_file($file, $to)
{
    global $wp_filesystem;
    if (!$wp_filesystem || !is_object($wp_filesystem)) {
        return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    }
    // Unzip can use a lot of memory, but not this much hopefully.
    wp_raise_memory_limit('admin');
    $needed_dirs = array();
    $to = trailingslashit($to);
    // Determine any parent directories needed (of the upgrade directory).
    if (!$wp_filesystem->is_dir($to)) {
        // Only do parents if no children exist.
        $path = preg_split('![/\\\\]!', untrailingslashit($to));
        for ($i = count($path); $i >= 0; $i--) {
            if (empty($path[$i])) {
                continue;
            }
            $dir = implode('/', array_slice($path, 0, $i + 1));
            if (preg_match('!^[a-z]:$!i', $dir)) {
                // Skip it if it looks like a Windows Drive letter.
                continue;
            }
            if (!$wp_filesystem->is_dir($dir)) {
                $needed_dirs[] = $dir;
            } else {
                break;
                // A folder exists, therefore we don't need to check the levels below this.
            }
        }
    }
    /**
     * Filters whether to use ZipArchive to unzip archives.
     *
     * @since 3.0.0
     *
     * @param bool $ziparchive Whether to use ZipArchive. Default true.
     */
    if (class_exists('ZipArchive', false) && apply_filters('unzip_file_use_ziparchive', true)) {
        $result = _unzip_file_ziparchive($file, $to, $needed_dirs);
        if (true === $result) {
            return $result;
        } elseif (is_wp_error($result)) {
            if ('incompatible_archive' !== $result->get_error_code()) {
                return $result;
            }
        }
    }
    // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
    return _unzip_file_pclzip($file, $to, $needed_dirs);
}

WordPress Version: 5.2

/**
 * Unzips a specified ZIP file to a location on the filesystem via the WordPress
 * Filesystem Abstraction.
 *
 * Assumes that WP_Filesystem() has already been called and set up. Does not extract
 * a root-level __MACOSX directory, if present.
 *
 * Attempts to increase the PHP memory limit to 256M before uncompressing. However,
 * the most memory required shouldn't be much larger than the archive itself.
 *
 * @since 2.5.0
 *
 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 *
 * @param string $file Full path and filename of ZIP archive.
 * @param string $to   Full path on the filesystem to extract archive to.
 * @return true|WP_Error True on success, WP_Error on failure.
 */
function unzip_file($file, $to)
{
    global $wp_filesystem;
    if (!$wp_filesystem || !is_object($wp_filesystem)) {
        return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    }
    // Unzip can use a lot of memory, but not this much hopefully.
    wp_raise_memory_limit('admin');
    $needed_dirs = array();
    $to = trailingslashit($to);
    // Determine any parent directories needed (of the upgrade directory).
    if (!$wp_filesystem->is_dir($to)) {
        // Only do parents if no children exist.
        $path = preg_split('![/\\\\]!', untrailingslashit($to));
        for ($i = count($path); $i >= 0; $i--) {
            if (empty($path[$i])) {
                continue;
            }
            $dir = implode('/', array_slice($path, 0, $i + 1));
            if (preg_match('!^[a-z]:$!i', $dir)) {
                // Skip it if it looks like a Windows Drive letter.
                continue;
            }
            if (!$wp_filesystem->is_dir($dir)) {
                $needed_dirs[] = $dir;
            } else {
                break;
                // A folder exists, therefore we don't need to check the levels below this.
            }
        }
    }
    /**
     * Filters whether to use ZipArchive to unzip archives.
     *
     * @since 3.0.0
     *
     * @param bool $ziparchive Whether to use ZipArchive. Default true.
     */
    if (class_exists('ZipArchive', false) && apply_filters('unzip_file_use_ziparchive', true)) {
        $result = _unzip_file_ziparchive($file, $to, $needed_dirs);
        if (true === $result) {
            return $result;
        } elseif (is_wp_error($result)) {
            if ('incompatible_archive' != $result->get_error_code()) {
                return $result;
            }
        }
    }
    // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
    return _unzip_file_pclzip($file, $to, $needed_dirs);
}

WordPress Version: 5.1

/**
 * Unzips a specified ZIP file to a location on the filesystem via the WordPress
 * Filesystem Abstraction.
 *
 * Assumes that WP_Filesystem() has already been called and set up. Does not extract
 * a root-level __MACOSX directory, if present.
 *
 * Attempts to increase the PHP memory limit to 256M before uncompressing. However,
 * the most memory required shouldn't be much larger than the archive itself.
 *
 * @since 2.5.0
 *
 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
 *
 * @param string $file Full path and filename of ZIP archive.
 * @param string $to   Full path on the filesystem to extract archive to.
 * @return true|WP_Error True on success, WP_Error on failure.
 */
function unzip_file($file, $to)
{
    global $wp_filesystem;
    if (!$wp_filesystem || !is_object($wp_filesystem)) {
        return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    }
    // Unzip can use a lot of memory, but not this much hopefully.
    wp_raise_memory_limit('admin');
    $needed_dirs = array();
    $to = trailingslashit($to);
    // Determine any parent dir's needed (of the upgrade directory)
    if (!$wp_filesystem->is_dir($to)) {
        //Only do parents if no children exist
        $path = preg_split('![/\\\\]!', untrailingslashit($to));
        for ($i = count($path); $i >= 0; $i--) {
            if (empty($path[$i])) {
                continue;
            }
            $dir = implode('/', array_slice($path, 0, $i + 1));
            if (preg_match('!^[a-z]:$!i', $dir)) {
                // Skip it if it looks like a Windows Drive letter.
                continue;
            }
            if (!$wp_filesystem->is_dir($dir)) {
                $needed_dirs[] = $dir;
            } else {
                break;
                // A folder exists, therefor, we dont need the check the levels below this
            }
        }
    }
    /**
     * Filters whether to use ZipArchive to unzip archives.
     *
     * @since 3.0.0
     *
     * @param bool $ziparchive Whether to use ZipArchive. Default true.
     */
    if (class_exists('ZipArchive', false) && apply_filters('unzip_file_use_ziparchive', true)) {
        $result = _unzip_file_ziparchive($file, $to, $needed_dirs);
        if (true === $result) {
            return $result;
        } elseif (is_wp_error($result)) {
            if ('incompatible_archive' != $result->get_error_code()) {
                return $result;
            }
        }
    }
    // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
    return _unzip_file_pclzip($file, $to, $needed_dirs);
}

WordPress Version: 4.6

/**
 * Unzips a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction.
 * Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present.
 *
 * Attempts to increase the PHP Memory limit to 256M before uncompressing,
 * However, The most memory required shouldn't be much larger than the Archive itself.
 *
 * @since 2.5.0
 *
 * @global WP_Filesystem_Base $wp_filesystem Subclass
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @return mixed WP_Error on failure, True on success
 */
function unzip_file($file, $to)
{
    global $wp_filesystem;
    if (!$wp_filesystem || !is_object($wp_filesystem)) {
        return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    }
    // Unzip can use a lot of memory, but not this much hopefully.
    wp_raise_memory_limit('admin');
    $needed_dirs = array();
    $to = trailingslashit($to);
    // Determine any parent dir's needed (of the upgrade directory)
    if (!$wp_filesystem->is_dir($to)) {
        //Only do parents if no children exist
        $path = preg_split('![/\\\\]!', untrailingslashit($to));
        for ($i = count($path); $i >= 0; $i--) {
            if (empty($path[$i])) {
                continue;
            }
            $dir = implode('/', array_slice($path, 0, $i + 1));
            if (preg_match('!^[a-z]:$!i', $dir)) {
                // Skip it if it looks like a Windows Drive letter.
                continue;
            }
            if (!$wp_filesystem->is_dir($dir)) {
                $needed_dirs[] = $dir;
            } else {
                break;
            }
            // A folder exists, therefor, we dont need the check the levels below this
        }
    }
    /**
     * Filters whether to use ZipArchive to unzip archives.
     *
     * @since 3.0.0
     *
     * @param bool $ziparchive Whether to use ZipArchive. Default true.
     */
    if (class_exists('ZipArchive', false) && apply_filters('unzip_file_use_ziparchive', true)) {
        $result = _unzip_file_ziparchive($file, $to, $needed_dirs);
        if (true === $result) {
            return $result;
        } elseif (is_wp_error($result)) {
            if ('incompatible_archive' != $result->get_error_code()) {
                return $result;
            }
        }
    }
    // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
    return _unzip_file_pclzip($file, $to, $needed_dirs);
}

WordPress Version: 4.4

/**
 * Unzips a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction.
 * Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present.
 *
 * Attempts to increase the PHP Memory limit to 256M before uncompressing,
 * However, The most memory required shouldn't be much larger than the Archive itself.
 *
 * @since 2.5.0
 *
 * @global WP_Filesystem_Base $wp_filesystem Subclass
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @return mixed WP_Error on failure, True on success
 */
function unzip_file($file, $to)
{
    global $wp_filesystem;
    if (!$wp_filesystem || !is_object($wp_filesystem)) {
        return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    }
    // Unzip can use a lot of memory, but not this much hopefully
    /** This filter is documented in wp-admin/admin.php */
    @ini_set('memory_limit', apply_filters('admin_memory_limit', WP_MAX_MEMORY_LIMIT));
    $needed_dirs = array();
    $to = trailingslashit($to);
    // Determine any parent dir's needed (of the upgrade directory)
    if (!$wp_filesystem->is_dir($to)) {
        //Only do parents if no children exist
        $path = preg_split('![/\\\\]!', untrailingslashit($to));
        for ($i = count($path); $i >= 0; $i--) {
            if (empty($path[$i])) {
                continue;
            }
            $dir = implode('/', array_slice($path, 0, $i + 1));
            if (preg_match('!^[a-z]:$!i', $dir)) {
                // Skip it if it looks like a Windows Drive letter.
                continue;
            }
            if (!$wp_filesystem->is_dir($dir)) {
                $needed_dirs[] = $dir;
            } else {
                break;
            }
            // A folder exists, therefor, we dont need the check the levels below this
        }
    }
    /**
     * Filter whether to use ZipArchive to unzip archives.
     *
     * @since 3.0.0
     *
     * @param bool $ziparchive Whether to use ZipArchive. Default true.
     */
    if (class_exists('ZipArchive', false) && apply_filters('unzip_file_use_ziparchive', true)) {
        $result = _unzip_file_ziparchive($file, $to, $needed_dirs);
        if (true === $result) {
            return $result;
        } elseif (is_wp_error($result)) {
            if ('incompatible_archive' != $result->get_error_code()) {
                return $result;
            }
        }
    }
    // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
    return _unzip_file_pclzip($file, $to, $needed_dirs);
}

WordPress Version: 4.3

/**
 * Unzips a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction.
 * Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present.
 *
 * Attempts to increase the PHP Memory limit to 256M before uncompressing,
 * However, The most memory required shouldn't be much larger than the Archive itself.
 *
 * @since 2.5.0
 *
 * @global WP_Filesystem_Base $wp_filesystem Subclass
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @return mixed WP_Error on failure, True on success
 */
function unzip_file($file, $to)
{
    global $wp_filesystem;
    if (!$wp_filesystem || !is_object($wp_filesystem)) {
        return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    }
    // Unzip can use a lot of memory, but not this much hopefully
    /** This filter is documented in wp-admin/admin.php */
    @ini_set('memory_limit', apply_filters('admin_memory_limit', WP_MAX_MEMORY_LIMIT));
    $needed_dirs = array();
    $to = trailingslashit($to);
    // Determine any parent dir's needed (of the upgrade directory)
    if (!$wp_filesystem->is_dir($to)) {
        //Only do parents if no children exist
        $path = preg_split('![/\\\\]!', untrailingslashit($to));
        for ($i = count($path); $i >= 0; $i--) {
            if (empty($path[$i])) {
                continue;
            }
            $dir = implode('/', array_slice($path, 0, $i + 1));
            if (preg_match('!^[a-z]:$!i', $dir)) {
                // Skip it if it looks like a Windows Drive letter.
                continue;
            }
            if (!$wp_filesystem->is_dir($dir)) {
                $needed_dirs[] = $dir;
            } else {
                break;
            }
            // A folder exists, therefor, we dont need the check the levels below this
        }
    }
    /**
     * Filter whether to use ZipArchive to unzip archives.
     *
     * @since 3.0.0
     *
     * @param bool $ziparchive Whether to use ZipArchive. Default true.
     */
    if (class_exists('ZipArchive') && apply_filters('unzip_file_use_ziparchive', true)) {
        $result = _unzip_file_ziparchive($file, $to, $needed_dirs);
        if (true === $result) {
            return $result;
        } elseif (is_wp_error($result)) {
            if ('incompatible_archive' != $result->get_error_code()) {
                return $result;
            }
        }
    }
    // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
    return _unzip_file_pclzip($file, $to, $needed_dirs);
}

WordPress Version: 3.9

/**
 * Unzips a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction.
 * Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present.
 *
 * Attempts to increase the PHP Memory limit to 256M before uncompressing,
 * However, The most memory required shouldn't be much larger than the Archive itself.
 *
 * @since 2.5.0
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @return mixed WP_Error on failure, True on success
 */
function unzip_file($file, $to)
{
    global $wp_filesystem;
    if (!$wp_filesystem || !is_object($wp_filesystem)) {
        return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    }
    // Unzip can use a lot of memory, but not this much hopefully
    /** This filter is documented in wp-admin/admin.php */
    @ini_set('memory_limit', apply_filters('admin_memory_limit', WP_MAX_MEMORY_LIMIT));
    $needed_dirs = array();
    $to = trailingslashit($to);
    // Determine any parent dir's needed (of the upgrade directory)
    if (!$wp_filesystem->is_dir($to)) {
        //Only do parents if no children exist
        $path = preg_split('![/\\\\]!', untrailingslashit($to));
        for ($i = count($path); $i >= 0; $i--) {
            if (empty($path[$i])) {
                continue;
            }
            $dir = implode('/', array_slice($path, 0, $i + 1));
            if (preg_match('!^[a-z]:$!i', $dir)) {
                // Skip it if it looks like a Windows Drive letter.
                continue;
            }
            if (!$wp_filesystem->is_dir($dir)) {
                $needed_dirs[] = $dir;
            } else {
                break;
            }
            // A folder exists, therefor, we dont need the check the levels below this
        }
    }
    /**
     * Filter whether to use ZipArchive to unzip archives.
     *
     * @since 3.0.0
     *
     * @param bool $ziparchive Whether to use ZipArchive. Default true.
     */
    if (class_exists('ZipArchive') && apply_filters('unzip_file_use_ziparchive', true)) {
        $result = _unzip_file_ziparchive($file, $to, $needed_dirs);
        if (true === $result) {
            return $result;
        } elseif (is_wp_error($result)) {
            if ('incompatible_archive' != $result->get_error_code()) {
                return $result;
            }
        }
    }
    // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
    return _unzip_file_pclzip($file, $to, $needed_dirs);
}

WordPress Version: 3.7

/**
 * Unzips a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction.
 * Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present.
 *
 * Attempts to increase the PHP Memory limit to 256M before uncompressing,
 * However, The most memory required shouldn't be much larger than the Archive itself.
 *
 * @since 2.5.0
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @return mixed WP_Error on failure, True on success
 */
function unzip_file($file, $to)
{
    global $wp_filesystem;
    if (!$wp_filesystem || !is_object($wp_filesystem)) {
        return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    }
    // Unzip can use a lot of memory, but not this much hopefully
    @ini_set('memory_limit', apply_filters('admin_memory_limit', WP_MAX_MEMORY_LIMIT));
    $needed_dirs = array();
    $to = trailingslashit($to);
    // Determine any parent dir's needed (of the upgrade directory)
    if (!$wp_filesystem->is_dir($to)) {
        //Only do parents if no children exist
        $path = preg_split('![/\\\\]!', untrailingslashit($to));
        for ($i = count($path); $i >= 0; $i--) {
            if (empty($path[$i])) {
                continue;
            }
            $dir = implode('/', array_slice($path, 0, $i + 1));
            if (preg_match('!^[a-z]:$!i', $dir)) {
                // Skip it if it looks like a Windows Drive letter.
                continue;
            }
            if (!$wp_filesystem->is_dir($dir)) {
                $needed_dirs[] = $dir;
            } else {
                break;
            }
            // A folder exists, therefor, we dont need the check the levels below this
        }
    }
    if (class_exists('ZipArchive') && apply_filters('unzip_file_use_ziparchive', true)) {
        $result = _unzip_file_ziparchive($file, $to, $needed_dirs);
        if (true === $result) {
            return $result;
        } elseif (is_wp_error($result)) {
            if ('incompatible_archive' != $result->get_error_code()) {
                return $result;
            }
        }
    }
    // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
    return _unzip_file_pclzip($file, $to, $needed_dirs);
}