Oihana PHP

makeAbsolute.php

Table of Contents

Functions

makeAbsolute()  : string
Turns a relative path into an absolute path in canonical form.

Functions

makeAbsolute()

Turns a relative path into an absolute path in canonical form.

makeAbsolute(string $path, string $basePath) : string

This function joins a relative path with an absolute base path to create a new absolute path. It also canonicalizes the result, which means:

  • Dot segments ("." and "..") are resolved.
  • All directory separators are converted to forward slashes ("/").
  • Redundant slashes are removed.

If the provided path is already absolute, it is simply canonicalized and returned, ignoring the base path.

The function correctly handles URI schemes (like "phar://" or "vfs://") by preserving the scheme from the base path in the final result.

Parameters
$path : string

The path to resolve. Can be relative or absolute.

$basePath : string

The absolute base path. Must not be empty.

Tags
throws
InvalidArgumentException

If the base path is empty or not an absolute path.

author

Marc Alcaraz (ekameleon)

since
1.0.0
example
// Basic usage with a Unix-style path
makeAbsolute('documents/../project/file.txt', '/home/user');
// => '/home/user/project/file.txt'

// Usage with a Windows-style path
makeAbsolute('data\\.\\config.ini', 'C:\\Users\\Test');
// => 'C:/Users/Test/data/config.ini'

// When the path is already absolute, the base path is ignored
makeAbsolute('/etc/app.conf', '/var/www');
// => '/etc/app.conf'

// Correctly handles URI schemes
makeAbsolute('src/bootstrap.php', 'phar:///usr/local/bin/composer.phar');
// => 'phar:///usr/local/bin/composer.phar/src/bootstrap.php'
Return values
string

The resulting absolute path in canonical form.


        
On this page

Search results