note 55014 deleted from function.realpath by danbrown
| From: | danbrown@php.net | Date: | Mon, 01 Jun 2009 13:03:19 +0000 |
| Subject: | note 55014 deleted from function.realpath by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156242@lists.php.net to get a copy of this message | ||
Note Submitter: phpnote at web-milk dot co dot uk
----
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;
}
?>