Combine directory and file name in PHP ( equivalent of Path.Combine in .Net)

$filepath = $path . DIRECTORY_SEPARATOR . $file;

Although in newer versions of PHP it doesn't matter which way the slashes go, so it is fine to always use forward slashes.

You can get a correct absolute path using realpath(), this will also remove things like extra unnecessary slashes and resolve references like ../. It will return false if the path is not valid.


I think the most clean and flexible way to do it would be using the join function plus the DIRECTORY_SEPARATOR constant:

$fullPath = join(DIRECTORY_SEPARATOR, array($directoryPath, $fileName));

Tags:

Php