note 31020 added to function.realpath
| From: | www dot e-xoops dot com at rack1 dot php dot net | Date: | Mon, 07 Apr 2003 06:47:13 +0000 |
| Subject: | note 31020 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-46300@lists.php.net to get a copy of this message | ||
Dunno if this will work in all cases ..so feel free to improve on it, but it seems to work fine
& should be ok with both windows & unix, even if stuff like realpath(), etc is disabled on
the server:
// We need this as a reference,
// so be sure to set it to the server root.
define(_SYS_ROOT, '/home/user/www');
// Compare the reference to a path
// & try & work out the real path
function real_path($path, $root=_SYS_ROOT) {
global $HTTP_SERVER_VARS;
// Windows/Unix
$root = str_replace('\\', '/', $root);
$array1 = explode('/', $root);
$array2 = explode('/', dirname($HTTP_SERVER_VARS['PHP_SELF']));
$array = array_merge($array1, $array2);
$array = array_unique($array);
foreach ($array as $key=>$value) {
if ( empty($value) ) {
unset($array[$key]);
}
}
$back = substr_count($path, '../');
$curr = substr_count($path, './');
$path = explode('/', $path);
if ( !empty($back) ) {
for ($i=0; $i<$back; $i++) {
array_shift($path);
array_pop($array);
}
} elseif ( !empty($curr) ) {
array_shift($path);
}
$array = array_merge($array, $path);
$path = join('/', $array);
// A preference
if ( substr($path, -1) != '/') {
$path .= '/';
}
// Windows/Unix
if ( (substr($path, 1) != '/') && !preg_match('#^[a-z]:#i', $path) ) {
$path = '/'.$path;
}
return $path;
}
echo real_path('../../test');
---------
Cheers, Half
--
http://www.php.net/manual/en/function.realpath.php
http://master.php.net/manage/user-notes.php?action=edit+31020
http://master.php.net/manage/user-notes.php?action=delete+31020
http://master.php.net/manage/user-notes.php?action=reject+31020