函数文档

_wp_check_existing_file_names()

💡 云策文档标注

概述

_wp_check_existing_file_names() 是一个辅助函数,用于检查给定的文件名是否可能与现有图像子尺寸文件名匹配。它通过正则表达式比较文件名,避免文件冲突。

关键要点

  • 函数检查文件名是否匹配现有文件,返回布尔值 true 或 false。
  • 参数包括 $filename(要检查的文件名)和 $files(目录中现有文件的数组)。
  • 使用正则表达式检测文件名模式,如 'filename-100x100.jpg' 或 'filename-scaled.png'。
  • 处理边缘情况,如文件名为 '.ext' 时返回 false。

代码示例

function _wp_check_existing_file_names( $filename, $files ) {
    $fname = pathinfo( $filename, PATHINFO_FILENAME );
    $ext   = pathinfo( $filename, PATHINFO_EXTENSION );

    // Edge case, file names like `.ext`.
    if ( empty( $fname ) ) {
        return false;
    }

    if ( $ext ) {
        $ext = ".$ext";
    }

    $regex = '/^' . preg_quote( $fname ) . '-(?:d+xd+|scaled|rotated)' . preg_quote( $ext ) . '$/i';

    foreach ( $files as $file ) {
        if ( preg_match( $regex, $file ) ) {
            return true;
        }
    }

    return false;
}

📄 原文内容

Helper function to check if a file name could match an existing image sub-size file name.

Parameters

$filenamestringrequired
The file name to check.
$filesarrayrequired
An array of existing files in the directory.

Return

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

Source

function _wp_check_existing_file_names( $filename, $files ) {
	$fname = pathinfo( $filename, PATHINFO_FILENAME );
	$ext   = pathinfo( $filename, PATHINFO_EXTENSION );

	// Edge case, file names like `.ext`.
	if ( empty( $fname ) ) {
		return false;
	}

	if ( $ext ) {
		$ext = ".$ext";
	}

	$regex = '/^' . preg_quote( $fname ) . '-(?:d+xd+|scaled|rotated)' . preg_quote( $ext ) . '$/i';

	foreach ( $files as $file ) {
		if ( preg_match( $regex, $file ) ) {
			return true;
		}
	}

	return false;
}

Changelog

Version Description
5.3.1 Introduced.