note 57016 added to function.realpath
| From: | bart at mediawave dot nl | Date: | Wed, 21 Sep 2005 18:31:40 +0000 |
| Subject: | note 57016 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95553@lists.php.net to get a copy of this message | ||
Here's another function that resolves references to '/./', '/../' and extra
'/' characters in the input path and returns the canonicalized pathname.
<?php
function cleanPath($path) {
$result = array();
// $pathA = preg_split('/[\/\\\]/', $path);
$pathA = explode('/', $path);
if (!$pathA[0])
$result[] = '';
foreach ($pathA AS $key => $dir) {
if ($dir == '..') {
if (end($result) == '..') {
$result[] = '..';
} elseif (!array_pop($result)) {
$result[] = '..';
}
} elseif ($dir && $dir != '.') {
$result[] = $dir;
}
}
if (!end($pathA))
$result[] = '';
return implode('/', $result);
}
echo 'input: ', $path = '..//./../dir4//./dir5/dir6/..//dir7/', '<br
/>';
echo 'output: ', cleanPath($path), '<br />';
?>
Will return:
input: ..//./../dir4//./dir5/dir6/..//dir7/
output: ../../dir4/dir5/dir7/
----
Manual Page -- http://www.php.net/manual/en/function.realpath.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57016
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57016&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57016&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57016&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57016&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57016&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57016&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57016&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57016&report=yes
Search -- http://master.php.net/manage/user-notes.php