note 55825 added to function.realpath
| From: | TyronMadlener at osu1 dot php dot net | Date: | Sun, 14 Aug 2005 19:53:14 +0000 |
| Subject: | note 55825 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93648@lists.php.net to get a copy of this message | ||
None of the functions provided here worked for me. All of them somehow had the problem with pathes
like '../../downloads/bar/../foo/..' or didn't provide relative paths.
So I coded my one that justs removes '.'-dirs from the paths and simplifies stuff like
'dir/..'
function SimplifyPath($path) {
$dirs = explode('/',$path);
for($i=0; $i<count($dirs);$i++) {
if($dirs[$i]=="." || $dirs[$i]=="") {
array_splice($dirs,$i,1);
$i--;
}
if($dirs[$i]=="..") {
$cnt = count($dirs);
$dirs=Simplify($dirs, $i);
$i-= $cnt-count($dirs);
}
}
return implode('/',$dirs);
}
function Simplify($dirs, $idx) {
if($idx==0) return $dirs;
if($dirs[$idx-1]=="..") Simplify($dirs, $idx-1);
else array_splice($dirs,$idx-1,2);
return $dirs;
}
I just wonder why all the other solutions are so complex and that mine so simple, because in general
the problem of simplifieng isn't that hard to solve. Well, I hope that there aren't any
cases on which my solution fails. Otherwise, feel free to improve :D
----
Manual Page -- http://www.php.net/manual/en/function.realpath.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55825
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55825&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55825&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55825&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55825&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55825&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55825&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55825&report=yes
Search -- http://master.php.net/manage/user-notes.php