函数文档

_wp_check_alternate_file_names()

💡 云策文档标注

概述

_wp_check_alternate_file_names() 是一个辅助函数,用于检查一组文件名是否可能与目录中现有文件冲突。它通过遍历文件名数组,检查文件是否存在或与现有文件匹配,返回布尔值指示冲突风险。

关键要点

  • 函数接受三个参数:$filenames(文件名数组)、$dir(目录路径)、$files(现有文件数组)。
  • 如果任一文件名在目录中存在或与现有文件匹配(通过 _wp_check_existing_file_names()),则返回 true 表示冲突。
  • 函数在 WordPress 5.8.1 版本引入,主要用于 wp_unique_filename() 等函数中,确保文件名唯一性。

代码示例

function _wp_check_alternate_file_names( $filenames, $dir, $files ) {
    foreach ( $filenames as $filename ) {
        if ( file_exists( $dir . $filename ) ) {
            return true;
        }

        if ( ! empty( $files ) && _wp_check_existing_file_names( $filename, $files ) ) {
            return true;
        }
    }

    return false;
}

📄 原文内容

Helper function to test if each of an array of file names could conflict with existing files.

Parameters

$filenamesstring[]required
Array of file names to check.
$dirstringrequired
The directory containing the files.
$filesarrayrequired
An array of existing files in the directory. May be empty.

Return

bool True if the tested file name could match an existing file, false otherwise.

Source

function _wp_check_alternate_file_names( $filenames, $dir, $files ) {
	foreach ( $filenames as $filename ) {
		if ( file_exists( $dir . $filename ) ) {
			return true;
		}

		if ( ! empty( $files ) && _wp_check_existing_file_names( $filename, $files ) ) {
			return true;
		}
	}

	return false;
}

Changelog

Version Description
5.8.1 Introduced.