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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With