Skip to content

Instantly share code, notes, and snippets.

@VladaHejda
Last active July 3, 2018 03:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VladaHejda/370393588abb7c287f7d to your computer and use it in GitHub Desktop.
Save VladaHejda/370393588abb7c287f7d to your computer and use it in GitHub Desktop.
PHP case-insensitive realpath()
<?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));
}
@ethaizone
Copy link

Thank you! This function work nicely. I used to get file from samba via php on unix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment