note 82010 deleted from function.realpath by felipe
| From: | felipe@php.net | Date: | Mon, 24 Mar 2008 22:08:12 +0000 |
| Subject: | note 82010 deleted from function.realpath by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137465@lists.php.net to get a copy of this message | ||
Note Submitter: caio dot ariede at gmail dot com
----
my relative-path function, not smallest but was very usefull for me.
<?php
function relativepath($dest_realpath = null)
{
if (is_null($dest_realpath)) $dest_realpath = realpath(dirname(__FILE__));
else $dest_realpath = realpath($dest_realpath);
if (!file_exists($dest_realpath)) return false;
$file = substr($dest_realpath, strrpos($dest_realpath, '/') + 1);
$dest = substr($dest_realpath, 0, strrpos($dest_realpath, '/') + 1);
$curr = getcwd() . '/';
$similar_func = create_function('$x,$y,$z', '$str = strlen($x) > strlen($y) ?
array($x, $y) : array($y, $x); list($str1, $str2) = $str; $r = "";
for($i=0;$str1[$i];$i++){ if ($str1[$i] != $str2[$i]) break; $r .= $str1[$i]; }; $r = substr($r, 0,
strrpos($r, $z)); return empty($r) || $r == $z ? null : $r;');
$similar = $similar_func($curr, $dest, '/');
if ($similar)
{
$back = substr_count(str_replace($similar, '', $curr), '/') - 1;
$dest = str_repeat('../', $back) . str_replace($similar . '/',
'', $dest);
}
else
{
$dest = str_repeat('../', substr_count($curr, '/') - 1) . substr($dest,
1);
}
return $dest . $file;
}
echo relativepath(dirname(__FILE__) . '/../test/example.php');
?>