php - 如何知道文件名在当前平台上是否有效?

标签 php filesystems

所有平台(可能还有文件系统)对于允许将哪些字符用作文件名或目录名都有不同的规则。此外,一些系统有一个文件名黑名单:例如在 Windows 上,com1 是一个无效的文件名。

有没有办法以编程方式了解在 PHP 中计算有效文件名的规则?

作为替代方案,除了 [0-9a-zA-Z] 之外,是否有一个可信任的安全字符列表,保证在任何系统上都有效?

请注意,基于尝试保存,如果失败,文件名无效的解决方案不适合我的用例。

最佳答案

已经回答好,Sanitizing strings to make them URL and filename safe?

I found this larger function in the 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("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
                   "}", "\\", "|", ";", ":", "\"", "'", "‘", "’", "“", "”", "–", "—",
                   "—", "–", ",", "<", ".", ">", "/", "?");
    $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 the 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);
}

Update Sept 2012

Alix Axel has done some incredible work in this area. His phunction framework includes several great text filters and transformations.

关于php - 如何知道文件名在当前平台上是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17629634/

相关文章:

php - Laravel 4 (Eloquent) 无法识别 ('date' ,'<' ,'NOW()' ) - 完全忽略它

android - phonegap html5 android 同步文件系统 IO

.net - Directory.EnumerateFiles => UnauthorizedAccessException

linux - 使用bash根据文件名(日期)移动日志文件

java - android 写入磁盘不可靠 - 写入 file.length !=expected.length

php - 电话号码改为2147483647?如何表示电话号码?

php - 哪个 PHP API 或库最适合从 HTML 转换为 PDF 和 DOCX?

javascript - 使用 PHP 将 Javascript 时间转换为 MySQL 格式

php - 如果 MYSQL 数据发生变化,有没有办法触发 PHP 文件系统缓存立即失效?

并发访问文件 linux