note 73194 deleted from function.realpath by danbrown
| From: | danbrown@php.net | Date: | Mon, 01 Jun 2009 13:02:15 +0000 |
| Subject: | note 73194 deleted from function.realpath by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156238@lists.php.net to get a copy of this message | ||
Note Submitter: Patrice Vanneufville
----
the precedent function does not work all the time : servers sometimes put a slash '/' at
the end of $_SERVER['DOCUMENT_ROOT']
// transforme un chemin d'image relatif en chemin html absolu
function htmlpath($relative_path) {
$realpath = str_replace("\\", "/", realpath($relative_path));
$root = preg_replace(',/$,', '', $_SERVER['DOCUMENT_ROOT']);
if (strlen($root) && strpos($realpath, $root)===0)
return substr($realpath, strlen($root));
$dir = dirname(!empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] :
(!empty($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] :
(!empty($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] :
str_replace('\\','/',__FILE__)
)));
return canonicalize($dir.'/'.$relative_path);
}
// retourne un chemin canonique a partir d'un chemin contenant des ../
function canonicalize($address) {
$address = str_replace("//", "/", $address);
$address = explode('/', $address);
$keys = array_keys($address, '..');
foreach($keys as $keypos => $key) array_splice($address, $key - ($keypos * 2 + 1), 2);
$address = implode('/', $address);
return preg_replace(',([^.])\./,', '\1', $address);
}