MENU
$root is the server root
$filePathInput is your file path
Source code
$cwd= getcwd();//server root
getRelativePath($yourPathInput, $cwd)
function getRelativePath($filePathInput, $root){
//$root= getcwd(); // get current working directory
$root= str_replace('\\','/',$root);// make all slashes forward
$filePathInput= str_replace('\\','/',$filePathInput);// make all slashes forward
$root = explode('/', $root);// root path array
$filePathInput = explode('/', $filePathInput);//input file path array
foreach($root as $level => $dir){
if($dir === $filePathInput[$level]){
unset($root[$level]);
unset($filePathInput[$level]);
}else{
$levelPos= $levelPos."../";
}
}
if(!$root){
$dirPath= implode('/', $filePathInput);// output down path
}else{
$dirPath= $levelPos.implode('/', $filePathInput);// output up path
}
return $dirPath;
}