sanitize_file_name()

清理文件名,用破折号替换空白。

sanitize_file_name(string $filename)

说明(Description)

删除某些操作系统上文件名中非法的特殊字符,以及在命令行操作时需要特殊转义的特殊字符。将空格和连续虚线替换为单个虚线。从文件名的开头和结尾修剪句点、短划线和下划线。无法保证此函数将返回允许上载的文件名。


参数(Parameters)

参数类型必填说明
$filename(string)必需要清理的文件名。

返回(Return)

(string)经过清理的文件名。


源码(Source)

/**
 * Sanitizes a filename, replacing whitespace with dashes.
 *
 * Removes special characters that are illegal in filenames on certain
 * operating systems and special characters requiring special escaping
 * to manipulate at the command line. Replaces spaces and consecutive
 * dashes with a single dash. Trims period, dash and underscore from beginning
 * and end of filename.
 *
 * @since 2.1.0
 *
 * @param string $filename The filename to be sanitized
 * @return string The sanitized filename
 */
function sanitize_file_name( $filename ) {
	$filename_raw = $filename;
	$special_chars = array("?", "[", "]", "/", "", "=", "<", "="">", ":", ";", ",", "'", """, "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", chr(0));
	/**
	 * Filter the list of characters to remove from a filename.
	 *
	 * @since 2.8.0
	 *
	 * @param array  $special_chars Characters to remove.
	 * @param string $filename_raw  Filename as it was passed into sanitize_file_name().
	 */
	$special_chars = apply_filters( 'sanitize_file_name_chars', $special_chars, $filename_raw );
	$filename = preg_replace( "#x{00a0}#siu", ' ', $filename );
	$filename = str_replace( $special_chars, '', $filename );
	$filename = str_replace( array( '%20', '+' ), '-', $filename );
	$filename = preg_replace( '/[
	 -]+/', '-', $filename );
	$filename = trim( $filename, '.-_' );
	// Split the filename into a base and extension[s]
	$parts = explode('.', $filename);
	// Return if only one extension
	if ( count( $parts ) <= 2="" )="" {="" *="" *="" filter="" a="" sanitized="" filename="" string.="" *="" *="" @since="" 2.8.0="" *="" *="" @param="" string="" $filename="" sanitized="" filename.="" *="" @param="" string="" $filename_raw="" the="" filename="" prior="" to="" sanitization.="" */="" return="" apply_filters(="" 'sanitize_file_name',="" $filename,="" $filename_raw="" );="" }="" process="" multiple="" extensions="" $filename="array_shift($parts);" $extension="array_pop($parts);" $mimes="get_allowed_mime_types();" *="" loop="" over="" any="" intermediate="" extensions.="" postfix="" them="" with="" a="" trailing="" underscore="" *="" if="" they="" are="" a="" 2="" -="" 5="" character="" long="" alpha="" string="" not="" in="" the="" extension="" whitelist.="" */="" foreach="" (="" (array)="" $parts="" as="" $part)="" {="" $filename="" .='.' .="" $part;="" if="" (="" preg_match("/^[a-za-z]{2,5}d?$/",="" $part)="" )="" {="" $allowed="false;" foreach="" (="" $mimes="" as="" $ext_preg=""> $mime_match ) {
				$ext_preg = '!^(' . $ext_preg . ')$!i';
				if ( preg_match( $ext_preg, $part ) ) {
					$allowed = true;
					break;
				}
			}
			if ( !$allowed )
				$filename .= '_';
		}
	}
	$filename .= '.' . $extension;
	/** This filter is documented in wp-includes/formatting.php */
	return apply_filters('sanitize_file_name', $filename, $filename_raw);
}
更新版本源码位置使用被使用
2.1.0wp-includes/formatting.php:200536
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索