【问题标题】:PHP skip case sensitivity in file extensionPHP在文件扩展名中跳过大小写敏感
【发布时间】:2013-01-07 08:48:33
【问题描述】:

我的数据库表中有一些行在目录中没有同步。

例如。在我的桌子上我有

image.png

但是在我的目录中我有

image.PNG

现在,我有问题要检查是否

file_exist

因为区分大小写。 我无法选择手动同步我的数据库和目录上的文件的选项,因为它太多了。

请问如何使用忽略文件类型敏感性的file_exist?

【问题讨论】:

  • 您可以轻松地将文件以小写形式存储或以正确的名称将它们放入数据库中。当然你可以每次都运行 throw 目录来查找真实姓名,但是它的错误解决方案,如果你去 throw 数千个文件,这可能会使 php 进程进入磁盘等待状态并丢弃请求。

标签: php file-exists


【解决方案1】:

有关更通用的解决方案,请参阅 php 文档中的 cmets:

General Solution

提供这个版本的功能:

/**
 * Alternative to file_exists() that will also return true if a file exists
 * with the same name in a different case.
 * eg. say there exists a file /path/product.class.php
 *  file_exists('/path/Product.class.php')
 *   => false
 *  similar_file_exists('/path/Product.class.php')
 *   => true
 */
function similar_file_exists($filename) {
  if (file_exists($filename)) {
    return true;
  }
  $dir = dirname($filename);
  $files = glob($dir . '/*');
  $lcaseFilename = strtolower($filename);
  foreach($files as $file) {
    if (strtolower($file) == $lcaseFilename) {
      return true;
    }
  }
  return false;
}

【讨论】:

    【解决方案2】:

    创建一个检查两个扩展的函数并直接使用它而不是 file_exist。

    【讨论】:

      【解决方案3】:

      我认为,您的根本原因是由于以下原因--
      如果您的数据库有“image.png”并且在目录“image.PNG”中,则意味着您的插入查询存在问题。
      这两个地方必须相同。建议以小写形式存储文件名,因为 Linux 系统区分大小写。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-03
        • 2016-05-30
        • 1970-01-01
        • 2016-04-14
        • 2021-01-16
        • 1970-01-01
        • 2016-09-12
        相关资源
        最近更新 更多