compile-fan
compile-fan

Reputation: 17625

How to get the "calculated" path in PHP?

$p = '/var/www/domain/a/b/../..';

How to write such a function func so that func($p) equals to '/var/www/domain/' ?

Upvotes: 0

Views: 94

Answers (1)

Aron Rotteveel
Aron Rotteveel

Reputation: 83203

Use realpath().

Example:

$p = '/var/www/domain/a/b/../..';
$q = realpath($p); // will result in /var/www/domain or FALSE on failure

Cases in which realpath() returns false, as quoted from the manual:

realpath() returns FALSE on failure, e.g. if the file does not exist.

Note:

The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return FALSE.

Upvotes: 4

Related Questions