Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuefekci/69c3da6167933af70ed0 to your computer and use it in GitHub Desktop.
Save tuefekci/69c3da6167933af70ed0 to your computer and use it in GitHub Desktop.
PHP Full-depth Case Insensitive file_exists() Function
<?php
/**
* Single level, Case Insensitive File Exists.
*
* Only searches one level deep. Based on
* https://gist.github.com/hubgit/456028
*
* @param string $file The file path to search for.
*
* @return string The path if found, FALSE otherwise.
*/
function fileExistsSingle($file)
{
if (file_exists($file) === TRUE) {
return $file;
}
$lowerfile = strtolower($file);
foreach (glob(dirname($file).'/*') as $file) {
if (strtolower($file) === $lowerfile) {
return $file;
}
}
return FALSE;
}//end fileExistsSingle()
/**
* Case Insensitive File Search.
*
* @param string $filePath The full path of the file to search for.
*
* @return string File path if valid, FALSE otherwise.
*/
function fileExistsCI($filePath)
{
if (file_exists($filePath) === TRUE) {
return $filePath;
}
// Split directory up into parts.
$dirs = explode('/', $filePath);
$len = count($dirs);
$dir = '/';
foreach ($dirs as $i => $part) {
$dirpath = fileExistsSingle($dir.$part);
if ($dirpath === FALSE) {
return FALSE;
}
$dir = $dirpath;
$dir .= (($i > 0) && ($i < $len - 1)) ? '/' : '';
}
return $dir;
}//end fileExistsCI()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment