note 73563 added to function.realpath
| From: | php-general at lists dot php dot net | Date: | Thu, 01 Mar 2007 16:17:13 +0000 |
| Subject: | note 73563 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123984@lists.php.net to get a copy of this message | ||
Tyron Madlener said "I just wonder why all the other solutions are so complex and that mine so
simple".
Funny. I wonder why yours is so complex (26 lines, 2 functions) when it can be done in half that
(13 lines, 1 function):
function rp($p) {
$p=explode('/', $p);
$o=array();
for ($i=0; $i<sizeof($p); $i++) {
if (''==$p[$i] || '.'==$p[$i]) continue;
if ('..'==$p[$i] && $i>0 && '..'!=$o[sizeof($o)-1]) {
array_pop($o);
continue;
}
array_push($o, $p[$i]);
}
return implode('/', $o);
}
With that, this:
print rp("../../xyz.txt")."\n";
print rp("a/b/c/d/../../xyz.txt")."\n";
print rp("a/b/c/d/../..//xyz.txt")."\n";
Prints:
../../xyz.txt
a/b/xyz.txt
a/b/xyz.txt
As expected.
----
Server IP: 69.147.83.197
Probable Submitter: 67.151.45.162
----
Manual Page -- http://www.php.net/manual/en/function.realpath.php
Edit -- https://master.php.net/note/edit/73563
Del: integrated -- https://master.php.net/note/delete/73563/integrated
Del: useless -- https://master.php.net/note/delete/73563/useless
Del: bad code -- https://master.php.net/note/delete/73563/bad+code
Del: spam -- https://master.php.net/note/delete/73563/spam
Del: non-english -- https://master.php.net/note/delete/73563/non-english
Del: in docs -- https://master.php.net/note/delete/73563/in+docs
Del: other reasons-- https://master.php.net/note/delete/73563
Reject -- https://master.php.net/note/reject/73563
Search -- https://master.php.net/manage/user-notes.php