note 77968 deleted from function.realpath by peehaa

From: Date: Sun, 28 Apr 2019 09:47:24 +0000
Subject: note 77968 deleted from function.realpath by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-210703@lists.php.net to get a copy of this message
Note Submitter: alban dot lopez+php dot net at gmail dot com ---- Here's a little function to return the shortest relative path between dest folder and current folder (you can be replace $_SERVER['PHP_SEFL'] by your variable folder) : <?php // if you run in /var/www/ echo UnRealPath ('./'); // return "./" echo UnRealPath ('./ajax-browser/AJAX-B/scripts/'); // return "./ajax-browser/AJAX-B/scripts/" echo UnRealPath ('/var/'); // return "./../" echo UnRealPath ('/opt/picasa/wine/'); // return "./../../opt/picasa/wine/" function UnRealPath ($dest) { $Ahere = explode ('/', realpath($_SERVER['PHP_SEFL'])); $Adest = explode ('/', realpath($dest)); $result = '.'; // le chemin retouné dois forcement commancé par ./ c'est le but while (implode ('/', $Adest) != implode ('/', $Ahere))// && count ($Adest)>0 && count($Ahere)>0 ) { if (count($Ahere)>count($Adest)) { array_pop($Ahere); $result .= '/..'; } else { array_pop($Adest); } } return str_replace('//', '/', $result.str_replace(implode ('/', $Adest), '', realpath($dest)).(@is_dir(realpath($dest))?'/':'')); } ?>

« previous php.notes (#210703) next »