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);
}
?>