sanitize_file_name [ WordPress Function ]
| Parameters: |
|
| Returns: |
|
| Defined at: |
|
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. Trim period, dash and underscore from beginning and end of filename.
Source
<?php
function sanitize_file_name( $filename ) {
$filename_raw = $filename;
$special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", chr(0));
$special_chars = apply_filters('sanitize_file_name_chars', $special_chars, $filename_raw);
$filename = str_replace($special_chars, '', $filename);
$filename = preg_replace('/[\s-]+/', '-', $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 )
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. Munge 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;
return apply_filters('sanitize_file_name', $filename, $filename_raw);
}
?>
Examples [ wp-snippets.com ]
Top Google zoekresultaten
- Function Reference/sanitize file name « WordPress Codex
Description. Sanitizes a filename replacing whitespace with dashes. Removes special characters that are illegal in filenames on certain operating systems and ...
codex.wordpress.org - sanitize_file_name Wordpress hook details -- Adam Brown, BYU ...
Detailed information about every action hook and filter used in WordPress. Makes Plugin API easier to use. Lists appearance, file location, and deprecation data ...
adambrown.info - PHPXRef 0.7 : WordPress : Function Reference: sanitize_file_name()
Function and Method Cross Reference. sanitize_file_name(). Defined at: /wp- includes/formatting.php -> line 800. Referenced 3 times: ...
phpxref.ftwr.co.uk - Quick question about sanitize_file_name - Old Nabble
Feb 17, 2012 ... Quick question about sanitize_file_name. I've been trying to generate filenames for some uploaded files using sanitize_file_name, and come ...
old.nabble.com