note 68330 added to function.realpath
| From: | egingell at sisna dot com | Date: | Sun, 23 Jul 2006 12:56:58 +0000 |
| Subject: | note 68330 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115056@lists.php.net to get a copy of this message | ||
<?
/*\\
* 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
*
\\*************/
?>
----
Server IP: 216.194.113.175
Probable Submitter: 71.35.197.135
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.realpath.php
Edit -- http://master.php.net/note/edit/68330
Del: integrated -- http://master.php.net/note/delete/68330/integrated
Del: useless -- http://master.php.net/note/delete/68330/useless
Del: bad code -- http://master.php.net/note/delete/68330/bad+code
Del: spam -- http://master.php.net/note/delete/68330/spam
Del: non-english -- http://master.php.net/note/delete/68330/non-english
Del: in docs -- http://master.php.net/note/delete/68330/in+docs
Del: other reasons-- http://master.php.net/note/delete/68330
Reject -- http://master.php.net/note/reject/68330
Search -- http://master.php.net/manage/user-notes.php