note 128080 added to function.realpath

From: Date: Wed, 04 Jan 2023 06:58:58 +0000
Subject: note 128080 added to function.realpath
Groups: php.notes 
Request: Send a blank email to php-notes+get-220036@lists.php.net to get a copy of this message
Sven Arduwie answer is not tested and does not replicate the behavior of realpath but is a close solution. This has been unit tested to be as close to realpath as possible but without the path having to actually exist in the system. This takes relative paths and realizes them properly based on actual current working directory, but everything can be virtual. eg. "../someVirtualDir/./virtualFile.jpg" <?php public static function virtualpath($path): string { $path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path); $len = strlen($path); $relative = strpos($path, DIRECTORY_SEPARATOR); if (!$len || ($len > 0 && $path[0] == '.') || $relative !== 0) { $path = getcwd() . DIRECTORY_SEPARATOR . $path; } $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); $absolutes = []; foreach ($parts as $part) { if ('.' == $part) { continue; } if ('..' == $part) { array_pop($absolutes); } else { $absolutes[] = $part; } } return DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $absolutes); } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.158.213 (proxied: 98.148.220.167) ---- Manual Page -- https://php.net/manual/en/function.realpath.php Edit -- https://main.php.net/note/edit/128080 Del: integrated -- https://main.php.net/note/delete/128080/integrated Del: useless -- https://main.php.net/note/delete/128080/useless Del: bad code -- https://main.php.net/note/delete/128080/bad+code Del: spam -- https://main.php.net/note/delete/128080/spam Del: non-english -- https://main.php.net/note/delete/128080/non-english Del: in docs -- https://main.php.net/note/delete/128080/in+docs Del: other reasons-- https://main.php.net/note/delete/128080 Reject -- https://main.php.net/note/reject/128080 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220036) next »