note 123783 added to function.realpath
| From: | plamen at dragiyski dot org | Date: | Tue, 16 Apr 2019 14:27:21 +0000 |
| Subject: | note 123783 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-210606@lists.php.net to get a copy of this message | ||
realpath() is just a system/library call to actual realpath() function supported by OS. It does not
work on a path as a string, but also resolves symlinks. The resulting path might significantly
differs from the input even when absolute path is given. No function in this notes resolves that.
The suggestion on the realpath man page is to look for an existing parent directory. Here is an
example:
<?php
function resolvePath($path) {
if(DIRECTORY_SEPARATOR !== '/') {
$path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
}
$search = explode('/', $path);
$search = array_filter($search, function($part) {
return $part !== '.';
});
$append = array();
$match = false;
while(count($search) > 0) {
$match = realpath(implode('/', $search));
if($match !== false) {
break;
}
array_unshift($append, array_pop($search));
};
if($match === false) {
$match = getcwd();
}
if(count($append) > 0) {
$match .= DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $append);
}
return $match;
}
?>
The result will retrieve absolute path for non-existing relative path. Even if a path does not
exists, there should be existing directory somewhere, for which the realpath could be obtained. If
this is not within the relative path (i.e. even current working directory does not exists), getcwd()
will retrieve absolute path, so some absolute path is returned (although in that case the PHP
process could have huge problems).
----
Server IP: 208.43.231.9
Probable Submitter: 185.85.3.194 (proxied: 2001:982:35ee:1:c34c:5472:9060:cc44)
----
Manual Page -- http://php.net/manual/en/function.realpath.php
Edit -- https://master.php.net/note/edit/123783
Del: integrated -- https://master.php.net/note/delete/123783/integrated
Del: useless -- https://master.php.net/note/delete/123783/useless
Del: bad code -- https://master.php.net/note/delete/123783/bad+code
Del: spam -- https://master.php.net/note/delete/123783/spam
Del: non-english -- https://master.php.net/note/delete/123783/non-english
Del: in docs -- https://master.php.net/note/delete/123783/in+docs
Del: other reasons-- https://master.php.net/note/delete/123783
Reject -- https://master.php.net/note/reject/123783
Search -- https://master.php.net/manage/user-notes.php