Skip to content

Instantly share code, notes, and snippets.

@johnsaigle
Last active June 27, 2017 19:02
Show Gist options
  • Select an option

  • Save johnsaigle/c37d401f6ea72cf56460ddb791636d53 to your computer and use it in GitHub Desktop.

Select an option

Save johnsaigle/c37d401f6ea72cf56460ddb791636d53 to your computer and use it in GitHub Desktop.
PHP realpath() alternative (free software)
<?php
/**
* Effectively resolve '..' characters in a file path
* @license GPLv3
*/
function resolvePath($path)
{
$resolvedPath = array();
// do some normalization
$path = str_replace('//', '/', $path);
$path_pieces = explode('/', $path);
foreach ($path_pieces as $piece) {
if ($piece == '.') {
continue;
}
if ($piece == '..') {
if (!is_array($resolvedPath)) {
error_log("ERROR: Resolved path not an array");
return "";
}
array_pop($resolvedPath);
continue;
}
array_push($resolvedPath, $piece);
}
$resolvedPath = implode('/', $resolvedPath);
return $resolvedPath;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment