返回介绍

unzip_file()

发布于 2017-09-11 11:01:23 字数 4590 浏览 1047 评论 0 收藏 0

unzip_file( string $file,  string $to )

Unzips a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction.


description

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.


参数

$file

(string) (Required) Full path and filename of zip archive

$to

(string) (Required) Full path on the filesystem to extract archive to


返回值

(mixed) WP_Error on failure, True on success


源代码

File: wp-admin/includes/file.php

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);
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: wp_raise_memory_limit()
  • wp-admin/includes/file.php: _unzip_file_ziparchive()
  • wp-admin/includes/file.php: _unzip_file_pclzip()
  • wp-admin/includes/file.php: unzip_file_use_ziparchive
  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: trailingslashit()
  • wp-includes/formatting.php: untrailingslashit()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/load.php: is_wp_error()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 5 more uses Hide more uses

Used By

  • wp-admin/includes/class-wp-upgrader.php: WP_Upgrader::unpack_package()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Example

    Basic example showing how to unzip the contents of a .zip file, that resides in the WordPress upload directory, to the same destination.

    
    <?php
    WP_Filesystem();
    $destination = wp_upload_dir();
    $destination_path = $destination['path'];
    $unzipfile = unzip_file( $destination_path.'/filename.zip', $destination_path);
       
       if ( $unzipfile ) {
          echo 'Successfully unzipped the file!';       
       } else {
          echo 'There was an error unzipping the file.';       
       }
    ?>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文