file_exists() 的 PHP 不区分大小写版本 [英] PHP Case Insensitive Version of file_exists()

查看:29
本文介绍了file_exists() 的 PHP 不区分大小写版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑在 PHP 中实现不区分大小写的 file_exists 函数的最快方法.我最好的办法是枚举目录中的文件并进行 strtolower() 与 strtolower() 的比较,直到找到匹配项?

I'm trying to think of the fastest way to implement a case insensitive file_exists function in PHP. Is my best bet to enumerate the file in the directory and do a strtolower() to strtolower() comparison until a match is found?

推荐答案

我使用了评论中的源代码来创建这个函数.如果找到则返回完整路径文件,否则返回 FALSE.

I used the source from the comments to create this function. Returns the full path file if found, FALSE if not.

对文件名中的目录名称不区分大小写.

Does not work case-insensitively on directory names in the filename.

function fileExists($fileName, $caseSensitive = true) {

    if(file_exists($fileName)) {
        return $fileName;
    }
    if($caseSensitive) return false;

    // Handle case insensitive requests            
    $directoryName = dirname($fileName);
    $fileArray = glob($directoryName . '/*', GLOB_NOSORT);
    $fileNameLowerCase = strtolower($fileName);
    foreach($fileArray as $file) {
        if(strtolower($file) == $fileNameLowerCase) {
            return $file;
        }
    }
    return false;
}

这篇关于file_exists() 的 PHP 不区分大小写版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆