Frank de Jonge Frank de Jonge - 1 year ago 173
PHP Question

What is the best way to resolve a relative path (like realpath) for non-existing files?

I'm trying to enforce a root directory in a filesystem abstraction. The problem I'm encountering is the following:

The API lets you read and write files, not only to local but also remote storages. So there's all kinds of normalisation going on under the hood. At the moment it doesn't support relative paths, so something like this isn't possible:

$filesystem->write('path/to/some/../relative/file.txt', 'file contents');

I want to be able to securely resolve the path so the output is would be:
As is stated in a github issue which was created for this bug/enhancement ( , it needs to do more that just splitting up segments and removing them accordingly.

Also, since the package handles remote filesystems and non-existing files, realpath is out of the question.

So, how should one go about when dealing with these paths?

Answer Source

I've resolved how to do this, this is my solution:

 * Normalize path
 * @param   string  $path
 * @param   string  $separator
 * @return  string  normalized path
public function normalizePath($path, $separator = '\\/')
    // Remove any kind of funky unicode whitespace
    $normalized = preg_replace('#\p{C}+|^\./#u', '', $path);

    // Path remove self referring paths ("/./").
    $normalized = preg_replace('#/\.(?=/)|^\./|\./$#', '', $normalized);

    // Regex for resolving relative paths
    $regex = '#\/*[^/\.]+/\.\.#Uu';

    while (preg_match($regex, $normalized)) {
        $normalized = preg_replace($regex, '', $normalized);

    if (preg_match('#/\.{2}|\.{2}/#', $normalized)) {
        throw new LogicException('Path is outside of the defined root, path: [' . $path . '], resolved: [' . $normalized . ']');

    return trim($normalized, $separator);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download