Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Case Insensitive Version of file_exists()

Tags:

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?

like image 621
Kirk Ouimet Avatar asked Oct 19 '10 01:10

Kirk Ouimet


1 Answers

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; } 
like image 149
Kirk Ouimet Avatar answered Sep 20 '22 15:09

Kirk Ouimet