本文介紹了file_exists() 的 PHP 不區(qū)分大小寫版本的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在考慮在 PHP 中實(shí)現(xiàn)不區(qū)分大小寫的 file_exists 函數(shù)的最快方法.我最好的辦法是枚舉目錄中的文件并進(jìn)行 strtolower() 與 strtolower() 的比較,直到找到匹配項(xiàng)?
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?
推薦答案
我使用了評論中的源代碼來創(chuàng)建這個(gè)函數(shù).如果找到則返回完整路徑文件,否則返回 FALSE.
I used the source from the comments to create this function. Returns the full path file if found, FALSE if not.
對文件名中的目錄名稱不區(qū)分大小寫.
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;
}
這篇關(guān)于file_exists() 的 PHP 不區(qū)分大小寫版本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!