本文介紹了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 不區分大小寫版本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!