Last active
June 27, 2017 19:02
-
-
Save johnsaigle/c37d401f6ea72cf56460ddb791636d53 to your computer and use it in GitHub Desktop.
PHP realpath() alternative (free software)
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 | |
| /** | |
| * 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