note 40695 added to function.realpath
| From: | ilgarm-at-azeronline dot com at rn2 dot php dot net | Date: | Thu, 11 Mar 2004 16:43:20 +0000 |
| Subject: | note 40695 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-66667@lists.php.net to get a copy of this message | ||
I've tested several examples given by other ppls but none were working, at least on windows. so
I spent time and wrote my own function for realpath( ) replacement that will work with non-existing
paths also.
Here it is. Please, check it, if you have time and let me know about the results. Thanks.
<?php
define( "_PL_OS_SEP", "/" );
define( "_CUR_OS", substr( php_uname( ), 0, 7 ) == "Windows" ? "Win" :
"_Nix" );
function checkCurrentOS( $_OS )
{
if ( strcmp( $_OS, _CUR_OS ) == 0 ) {
return true;
}
return false;
}
function isRelative( $_dir )
{
if ( checkCurrentOS( "Win" ) ) {
return ( preg_match( "/^\w+:/", $_dir ) <= 0 );
}
else {
return ( preg_match( "/^\//", $_dir ) <= 0 );
}
}
function unifyPath( $_path )
{
if ( checkCurrentOS( "Win" ) ) {
return str_replace( "\\", _PL_OS_SEP, $_path );
}
return $_path;
}
function getRealpath( $_path )
{
/*
* This is the starting point of the system root.
* Left empty for UNIX based and Mac.
* For Windows this is drive letter and semicolon.
*/
$__path = $_path;
if ( isRelative( $_path ) ) {
$__curdir = unifyPath( realpath( "." ) . _PL_OS_SEP );
$__path = $__curdir . $__path;
}
$__startPoint = "";
if ( checkCurrentOS( "Win" ) ) {
list( $__startPoint, $__path ) = explode( ":", $__path, 2 );
$__startPoint .= ":";
}
# From now processing is the same for WIndows and Unix, and hopefully for others.
$__realparts = array( );
$__parts = explode( _PL_OS_SEP, $__path );
for ( $i = 0; $i < count( $__parts ); $i++ ) {
if ( strlen( $__parts[ $i ] ) == 0 || $__parts[ $i ] == "." ) {
continue;
}
if ( $__parts[ $i ] == ".." ) {
if ( count( $__realparts ) > 0 ) {
array_pop( $__realparts );
}
}
else {
array_push( $__realparts, $__parts[ $i ] );
}
}
return $__startPoint . _PL_OS_SEP . implode( _PL_OS_SEP, $__realparts );
}
echo "getRealpath ../../x: ". getRealpath( "../../x" ) .
"<BR>\n";
echo "getRealpath ./../x: ". getRealpath( "./../x" ) . "<BR>\n";
echo "getRealpath x: ". getRealpath( "x" ) . "<BR>\n";
echo "getRealpath /../x: ". getRealpath( "/../x" ) . "<BR>\n";
echo "getRealpath d:/../../x: ". getRealpath( "d:/../../x" ) .
"<BR>\n";
echo "getRealpath ./../xx/xxx/.////../yyy: ". getRealpath(
"./../xx/xxx/.////../yyy" ) . "<BR>\n";
?>
----
Manual Page -- http://www.php.net/manual/en/function.realpath.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40695
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40695&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40695&report=yes
Search -- http://master.php.net/manage/user-notes.php