PHP大小写不敏感版本file_exists() [英] PHP Case Insensitive Version of file_exists()
本文介绍了PHP大小写不敏感版本file_exists()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图想到在PHP中实现不区分大小写的file_exists函数的最快方法。我最好的办法是枚举目录中的文件,然后执行strtolower()来比较strtolower(),直到找到匹配的结果为止。
我用注释来创建这个函数。返回完整路径文件(如果找到),否则返回FALSE。对文件名中的目录名不区分大小写。
函数fileExists($ fileName,$ caseSensitive = true){
if(file_exists($ fileName)){
return $ fileName ;
}
if($ caseSensitive)return false;
//处理不区分大小写的请求
$ directoryName = dirname($ fileName);
$ fileArray = glob($ directoryName。'/ *',GLOB_NOSORT);
$ fileNameLowerCase = strtolower($ fileName);
foreach($ fileArray as $ file){
if(strtolower($ file)== $ fileNameLowerCase){
return $ file;
}
}
return false;
}
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?
解决方案
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;
}
这篇关于PHP大小写不敏感版本file_exists()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文