note 31020 deleted from function.realpath by aidan

From: Date: Sun, 29 Aug 2004 21:51:12 +0000
Subject: note 31020 deleted from function.realpath by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75613@lists.php.net to get a copy of this message
Note Submitter: www.e-xoops.com ---- 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

« previous php.notes (#75613) next »