函数文档

wp_zip_file_is_valid()

💡 云策文档标注

概述

wp_zip_file_is_valid() 函数用于检查指定文件是否为有效的 ZIP 文件。它不验证文件是否存在,非存在文件也会返回 false。

关键要点

  • 函数接受一个必需参数 $file,表示 ZIP 文件的完整路径。
  • 返回布尔值,指示文件是否为有效 ZIP 文件。
  • 内部实现优先使用 ZipArchive,若不可用或出错则回退到 PclZip。
  • 相关 Hook:apply_filters( 'unzip_file_use_ziparchive', bool $ziparchive ) 用于过滤是否使用 ZipArchive 解压。
  • 自 WordPress 6.4.4 版本引入。

代码示例

require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';

$archive          = new PclZip( $file );
$archive_is_valid = is_array( $archive->properties() );

return $archive_is_valid;

📄 原文内容

Determines whether the given file is a valid ZIP file.

Description

This function does not test to ensure that a file exists. Non-existent files are not valid ZIPs, so those will also return false.

Parameters

$filestringrequired
Full path to the ZIP file.

Return

bool Whether the file is a valid ZIP file.

Source

			$archive->close();
			return true;
		}
	}

	// Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
	require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';

	$archive          = new PclZip( $file );
	$archive_is_valid = is_array( $archive->properties() );

	return $archive_is_valid;
}

/**
 * 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

Hooks

apply_filters( ‘unzip_file_use_ziparchive’, bool $ziparchive )

Filters whether to use ZipArchive to unzip archives.

Changelog

Version Description
6.4.4 Introduced.