note 38611 deleted from ref.filesystem by googleguy

From: Date: Tue, 22 Oct 2013 06:51:49 +0000
Subject: note 38611 deleted from ref.filesystem by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196680@lists.php.net to get a copy of this message
Note Submitter: http://iubito.free.fr ---- Here is a function I wrote to get the relative path between 2 files or directory. We suppose that paths are wrotten in Unix format (/ instead of windows \\) <?php /** * Return the relative path between two paths / Retourne le chemin relatif entre 2 chemins * * If $path2 is empty, get the current directory (getcwd). * @return string */ function relativePath($path1, $path2='') { if ($path2 == '') { $path2 = $path1; $path1 = getcwd(); } //Remove starting, ending, and double / in paths $path1 = trim($path1,'/'); $path2 = trim($path2,'/'); while (substr_count($path1, '//')) $path1 = str_replace('//', '/', $path1); while (substr_count($path2, '//')) $path2 = str_replace('//', '/', $path2); //create arrays $arr1 = explode('/', $path1); if ($arr1 == array('')) $arr1 = array(); $arr2 = explode('/', $path2); if ($arr2 == array('')) $arr2 = array(); $size1 = count($arr1); $size2 = count($arr2); //now the hard part :-p $path=''; for($i=0; $i<min($size1,$size2); $i++) { if ($arr1[$i] == $arr2[$i]) continue; else $path = '../'.$path.$arr2[$i].'/'; } if ($size1 > $size2) for ($i = $size2; $i < $size1; $i++) $path = '../'.$path; else if ($size2 > $size1) for ($i = $size1; $i < $size2; $i++) $path .= $arr2[$i].'/'; return $path; } ?> Enjoy ! :-)

« previous php.notes (#196680) next »