_wp_check_existing_file_names

函式
_wp_check_existing_file_names ( $filename, $files )
Access
Private
引數
  • (string) $filename The file name to check.
    Required:
  • (array) $files An array of existing files in the directory.
    Required:
返回值
  • (bool) True if the tested file name could match an existing file, false otherwise.
定義位置
相關方法
_wp_check_alternate_file_nameswp_check_filetypewp_check_for_changed_dateswp_using_themeswp_unique_filename
引入
5.3.1
棄用
-

WordPress中的_wp_check_existing_file_names函式是用來檢查WordPress媒體庫中是否已經存在一個與被上傳的檔案同名的檔案。它接受兩個引數,檔名和父文章的ID,並返回檔名,如果有必要的話,還附加一個唯一的字尾。

幫助函式,檢查檔名是否可以與現有的影象子尺寸檔名匹配。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >