miho
miho

Reputation: 12085

Absolute path from relative path and an anchor file in PHP

How can I compute the absolute path of a file, when I've got a relative path and another absolute path which should act as an anchor point.

For example:

$anchor = '/tmp/uploads/abcd1234/import.xml';
$relative = 'main_image.png';
$abs = foo($anchor, $relative); # fn `foo` is what I'm looking for
assert($abs == '/tmp/uploads/abcd1234/main_image.png');

or slightly more complicated:

$anchor = '/tmp/uploads/abcd1234/import.xml';
$relative = '../resource/dummy.jpg';
$abs = foo($anchor, $relative); # fn `foo` is what I'm looking for
assert($abs == '/tmp/uploads/resource/dummy.jpg');

Upvotes: 0

Views: 50

Answers (1)

Dinu
Dinu

Reputation: 1524

realpath(dirname($anchor).'/'.$relative)

Upvotes: 1

Related Questions