How to find out if the file name is really on the current platform?

All platforms (and possibly file systems) have different rules regarding what characters are allowed as a file or directory name. In addition, some systems have a blacklist of file names: for example, on Windows, com1 is an invalid file name.

Is there a way to programmatically know the rules for calculating a valid file name in PHP?

Alternatively, is there a reliable list of safe characters that are guaranteed to be valid for any system other than [0-9a-zA-Z] ?

Please note that a solution based on an attempt to save, if it fails, the file name is invalid, not acceptable for my use.

+4
source share
1 answer

Already answered well, Sanitize strings to make their URL and file name safe?

I found this great function in Chyrp code:

 /** * Function: sanitize * Returns a sanitized string, typically for URLs. * * Parameters: * $string - The string to sanitize. * $force_lowercase - Force the string to lowercase? * $anal - If set to *true*, will remove all non-alphanumeric characters. */ function sanitize($string, $force_lowercase = true, $anal = false) { $strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]", "}", "\\", "|", ";", ":", "\"", "'", "&#8216;", "&#8217;", "&#8220;", "&#8221;", "&#8211;", "&#8212;", "รขโ‚ฌ"", "รขโ‚ฌ"", ",", "<", ".", ">", "/", "?"); $clean = trim(str_replace($strip, "", strip_tags($string))); $clean = preg_replace('/\s+/', "-", $clean); $clean = ($anal) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ; return ($force_lowercase) ? (function_exists('mb_strtolower')) ? mb_strtolower($clean, 'UTF-8') : strtolower($clean) : $clean; } 

and this one in wordpress code

 /** * 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. * * @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("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}"); $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, '.-_'); return apply_filters('sanitize_file_name', $filename, $filename_raw); } 

September 2012 Update

Alix Axel has done some incredible work in this area. Its frame structure includes some great text filters and transformations.

+2
source

Source: https://habr.com/ru/post/1491254/


All Articles