note 55014 added to function.realpath
| From: | phpnote at web-milk dot co dot uk | Date: | Fri, 22 Jul 2005 14:01:24 +0000 |
| Subject: | note 55014 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-92298@lists.php.net to get a copy of this message | ||
I have written this function so that it does not matter whether the folder exists on the system. It
simply traverses the given path moving up directories at every "/.." that it encounters.
Though this can be used with a file at the end I would be cautious about using it with a query
attached as any "/.." in the query will remove the text proir until the previous
"/".
<?php
function htmlpath($relative_path)
{
$realpath = '';
$q = 0;
// Remove any ./
$relative_path = str_replace('/./', '/', $relative_path);
// Remove trailing /
if ($relative_path[strlen($relative_path)-1] == '/')
{
$relative_path = substr($relative_path, 0, -1);
}
$p = strpos($relative_path, '/..', $q);
while ($p !== false)
{
// Get the next part of the path
if ($p != $q) $realpath .= substr($relative_path, $q, $p);
// Find the edge of the previous directory
$i = strrpos($realpath, '/');
if ($i === false)
{
return false; // Not enough directories to go up any further.
}
// Remove the directory
$realpath = substr($realpath, 0, $i);
// Take out the "/.."
$relative_path = substr($relative_path, 0, $p) . substr($relative_path, $p+3);
// Find the next "/.."
$q = $p;
$p = strpos($relative_path, '/..', $q);
}
// Get the rest of the relative path.
$realpath .= substr($relative_path, $q);
return $realpath;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.realpath.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55014
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55014&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55014&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55014&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55014&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55014&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55014&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55014&report=yes
Search -- http://master.php.net/manage/user-notes.php