joinPath() - Combines Multiple Path Fragments

This function will combine all the path fragments provided as the argument and return the result. joinPath() will use the correct path separator for the current platform.


Each argument is a fragment of the result path. You can provide as many arguments as needed.


joinPath("/var/", "www", "html", "index.php");
Returns /var/www/html/index.php
joinPath("/var/", "/www/", "html/", "index.php");
Returns /var/www/html/index.php - excess '/' are not an issue
joinPath("/var/", "/www/", "", "html/", "", "index.php");
Again, returns /var/www/html/index.php - empty fragments are handled correctly


 * Takes one or more file names and combines them, using the correct path separator for the 
 *         current platform and then return the result.
 * Example: joinPath('/var','www/html/','try.php'); // returns '/var/www/html/try.php'
 * Link:
function joinPath() {
$path '';
$arguments func_get_args();
$args = array();
$arguments as $a) if($a !== ''$args[] = $a;//Removes the empty elements
$arg_count count($args);
$i=0$i<$arg_count$i++) {
$folder $args[$i];
$i != and $folder[0] == DIRECTORY_SEPARATOR$folder substr($folder,1); //Remove the first char if it is a '/' - and its not in the first argument
if($i != $arg_count-and substr($folder,-1) == DIRECTORY_SEPARATOR$folder substr($folder,0,-1); //Remove the last char - if its not in the last argument
$path .= $folder;
$i != $arg_count-1$path .= DIRECTORY_SEPARATOR//Add the '/' if its not the last element.
Subscribe to Feed