note 68330 deleted from function.realpath by nlopess
| From: | nlopess@php.net | Date: | Sun, 23 Jul 2006 12:59:16 +0000 |
| Subject: | note 68330 deleted from function.realpath by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-115057@lists.php.net to get a copy of this message | ||
Note Submitter: egingell at sisna dot com
Reason: useless
----
<?
/*\\
* string real_path(string $path, bool $no_check_path)
*
* Resolves all '.' and '..' symlinks in the given $path and returns the
modified path.
* If $no_check_path is true, the existence of directories along the $path are irrelevant.
\\*/
function real_path($path, $no_check_path = false) {
if ($no_check_path === false) {
return realpath($path);
} else {
$path = str_replace('\\\\', '/', $path);
$path_parts = explode('/', $path);
// Continue looping until all symlinks ('.' and '..') are resolved.
// This also eliminates the need to do any looping if the string $path does not contain any
symlinks.
while (in_array('.', $path_parts) || in_array('..', $path_parts)) {
// Note: This function did not resolve all symlinks until I put in the "break"
statements.
foreach($path_parts AS $i => $part) {
if($part == '.') {
// Delete the symlink and break out of the foreach()
unset($path_parts[$i]);
break;
}
if($part == '..') {
// Delete the symlink and the directory it's referencing and break out of the foreach()
unset($path_parts[$i], $path_parts[$i - 1]);
break;
}
}
// Reset the indexes since there will be gaps (ie. keys before 0,1,2,3 and keys after 0,2,3).
$path_parts = explode('/', implode('/', $path_parts));
}
// Return the final result as a string.
return implode('/', $path_parts);
}
}
$path = '/usr/bin/./s/../g/../man/..';
echo 'Original: ' . $path . '<br />
New: ' . real_path($path, true);
/** OUTPUT: **\\
*
* Original: /usr/bin/./s/../g/../man/..
* New: /usr/bin
*
\\*************/
?>