, realpath(), :
- / Windows,
c:. - .
- .
- . A) , B) FALSE C) .
- .
NB: this approach uses regular expressions, which are known to be expensive CPUs, so I like the method using arrays from http://www.php.net/manual/en/function.realpath.php#84012 .
define('FILE_EXISTENCE_CHECK', 0);
define('FILE_EXISTENCE_CHECK_REQUIRE_FILE', 1);
define('FILE_EXISTENCE_CHECK_SKIP', 2);
define('IS_WINDOWS', preg_match('#WIN(DOWS|\d+|_?NT)#i', PHP_OS));
define('DIR_SEP', DIRECTORY_SEPARATOR);
define('PREG_DIR_SEP', preg_quote(DIR_SEP));
function _realpath($path = NULL, $options = array()) {
$options = array_merge(array(
'file_existence_check' => FILE_EXISTENCE_CHECK_REQUIRE_FILE,
'follow_link' => FALSE,
), $options);
$path = $path ? $path : getcwd();
$path = preg_replace('#[\\\/]#', DIR_SEP, $path);
$path = preg_replace('#' . PREG_DIR_SEP . '(\.?' . PREG_DIR_SEP . ')+#', DIR_SEP, $path);
while (preg_match('#^(.*?)' . PREG_DIR_SEP . '[^' . PREG_DIR_SEP . ']+' . PREG_DIR_SEP . '\.\.($|' . PREG_DIR_SEP . '.*)#', $path, $m)) {
$path = $m[1] . $m[2];
}
$path = rtrim($path, DIR_SEP);
if (IS_WINDOWS) {
if (preg_match('#^([a-z]:)(.*)#', $path, $m)) {
$path = strtoupper($m[1]) . $m[2];
}
elseif ($path[0] === DIR_SEP) {
$path = substr(getcwd(), 0, 2) . $path;
}
}
else {
if (preg_match('#^[A-Z]:(' . PREG_DIR_SEP . '.*)#i', $path, $m)) {
$path = $m[1];
}
}
if (!preg_match('#^([A-Z]:)?' . PREG_DIR_SEP . '#', $path)) {
$path = getcwd() . DIR_SEP . $path;
}
if ($options['file_existence_check'] !== DSC_FILE_EXISTENCE_CHECK_SKIP && !file_exists($path)) {
if ($options['file_existence_check'] === DSC_FILE_EXISTENCE_CHECK) {
return FALSE;
}
else {
dsc_print_error('File does not exist: ' . $path);
}
}
if (!empty($options['follow_link']) && file_exists($path)) {
$path = readlink($path);
}
return $path;
}
source
share