sanitize_file_name()
sanitize_file_name( string $filename ) 清理文件名,用破折号替换空白。S…
sanitize_file_name( string $filename )
清理文件名,用破折号替换空白。
Sanitizes a filename, replacing whitespace with dashes.
目录锚点:#说明#参数#返回#源码
说明(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.0 | wp-includes/formatting.php:2005 | 3 | 6 |
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!