note 82010 added to function.realpath
| From: | caio dot ariede at gmail dot com | Date: | Mon, 24 Mar 2008 16:41:14 +0000 |
| Subject: | note 82010 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137456@lists.php.net to get a copy of this message | ||
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');
?>
----
Server IP: 69.147.83.197
Probable Submitter: 201.68.33.181
----
Manual Page -- http://www.php.net/manual/en/function.realpath.php
Edit -- https://master.php.net/note/edit/82010
Del: integrated -- https://master.php.net/note/delete/82010/integrated
Del: useless -- https://master.php.net/note/delete/82010/useless
Del: bad code -- https://master.php.net/note/delete/82010/bad+code
Del: spam -- https://master.php.net/note/delete/82010/spam
Del: non-english -- https://master.php.net/note/delete/82010/non-english
Del: in docs -- https://master.php.net/note/delete/82010/in+docs
Del: other reasons-- https://master.php.net/note/delete/82010
Reject -- https://master.php.net/note/reject/82010
Search -- https://master.php.net/manage/user-notes.php