Last active
July 3, 2018 03:55
-
-
Save VladaHejda/370393588abb7c287f7d to your computer and use it in GitHub Desktop.
PHP case-insensitive realpath()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Case-insensitive realpath() | |
* @param string $path | |
* @return string|false | |
*/ | |
function realpathi($path) | |
{ | |
$me = __METHOD__; | |
$path = rtrim(preg_replace('#[/\\\\]+#', DIRECTORY_SEPARATOR, $path), DIRECTORY_SEPARATOR); | |
$realPath = realpath($path); | |
if ($realPath !== false) { | |
return $realPath; | |
} | |
$dir = dirname($path); | |
if ($dir === $path) { | |
return false; | |
} | |
$dir = $me($dir); | |
if ($dir === false) { | |
return false; | |
} | |
$search = strtolower(basename($path)); | |
$pattern = ''; | |
for ($pos = 0; $pos < strlen($search); $pos++) { | |
$pattern .= sprintf('[%s%s]', $search[$pos], strtoupper($search[$pos])); | |
} | |
return current(glob($dir . DIRECTORY_SEPARATOR . $pattern)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! This function work nicely. I used to get file from samba via php on unix.