Oihana PHP

makeRelative.php

Table of Contents

Functions

makeRelative()  : string
Turns an absolute path into a path relative to another absolute path.

Functions

makeRelative()

Turns an absolute path into a path relative to another absolute path.

makeRelative(string $path, string $basePath) : string
Parameters
$path : string

The absolute path to convert.

$basePath : string

The absolute path to which the conversion should be relative.

Tags
throws
InvalidArgumentException

If paths cannot be compared (e.g., different roots, or not absolute).

example
// --- Basic Unix cases ---

// The target path is a subdirectory
makeRelative('/var/www/project/app', '/var/www/project');
// => 'app'

// Navigating to a parent then to a sibling directory
makeRelative('/var/www/assets', '/var/www/project/app');
// => '../../assets'

// The paths are identical
makeRelative('/var/www', '/var/www');
// => ''

// --- Specific cases ---

// Navigating from the root directory
makeRelative('/home/user/documents', '/');
// => 'home/user/documents'

// Example with a Windows path
makeRelative('C:/Users/Test/Documents', 'C:/Users/Test/Downloads');
// => '../Documents'

// Example with a URI scheme (phar)
makeRelative('phar:///app/src/controller', 'phar:///app/src/model');
// => '../controller'
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The relative path.


        
On this page

Search results