wp_check_filetype

函式
wp_check_filetype ( $filename, $mimes = null )
引數
  • (string) $filename File name or path.
    Required:
  • (string[]) $mimes Optional. Array of allowed mime types keyed by their file extension regex.
    Required:
    Default: null
返回值
  • (array) { Values for the extension and mime type. @type string|false $ext File extension, or false if the file doesn't match a mime type. @type string|false $type File mime type, or false if the file doesn't match a mime type. }
定義位置
相關方法
wp_check_filetype_and_extwp_cache_deletewp_checkdatewp_match_mime_typeswp_get_mime_types
引入
2.0.4
棄用
-

wp_check_filetype: 這是一個檢查某檔案型別是否允許上傳到網站的函式。它可以用來確保只有某些檔案型別被允許上傳到網站。

從檔名中獲取檔案型別。

如果需要,你可以選擇性地定義mime陣列。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_check_filetype( $filename, $mimes = null ) {
if ( empty( $mimes ) ) {
$mimes = get_allowed_mime_types();
}
$type = false;
$ext = false;
foreach ( $mimes as $ext_preg => $mime_match ) {
$ext_preg = '!.(' . $ext_preg . ')$!i';
if ( preg_match( $ext_preg, $filename, $ext_matches ) ) {
$type = $mime_match;
$ext = $ext_matches[1];
break;
}
}
return compact( 'ext', 'type' );
}
function wp_check_filetype( $filename, $mimes = null ) { if ( empty( $mimes ) ) { $mimes = get_allowed_mime_types(); } $type = false; $ext = false; foreach ( $mimes as $ext_preg => $mime_match ) { $ext_preg = '!.(' . $ext_preg . ')$!i'; if ( preg_match( $ext_preg, $filename, $ext_matches ) ) { $type = $mime_match; $ext = $ext_matches[1]; break; } } return compact( 'ext', 'type' ); }
function wp_check_filetype( $filename, $mimes = null ) {
	if ( empty( $mimes ) ) {
		$mimes = get_allowed_mime_types();
	}
	$type = false;
	$ext  = false;

	foreach ( $mimes as $ext_preg => $mime_match ) {
		$ext_preg = '!.(' . $ext_preg . ')$!i';
		if ( preg_match( $ext_preg, $filename, $ext_matches ) ) {
			$type = $mime_match;
			$ext  = $ext_matches[1];
			break;
		}
	}

	return compact( 'ext', 'type' );
}

常見問題

FAQs
檢視更多 >