note 75992 deleted from function.realpath by peehaa
| From: | peehaa@php.net | Date: | Sun, 28 Apr 2019 09:48:33 +0000 |
| Subject: | note 75992 deleted from function.realpath by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210710@lists.php.net to get a copy of this message | ||
Note Submitter: richpageau at yahoo dot co dot uk
----
This is my attempt at writing a realpath replacement. I needed to to run some Adobe code on a server
with realpath disabled and this seemed to do the job. It is written for a unix server, I suppose it
could be made cross platform using DIRECTORY_SEPARATOR. (With thanks to Marc Noirot for his code).
<?php
function myRealPath($path) {
// check if path begins with "/" ie. is absolute
// if it isnt concat with script path
if (strpos($path,"/") !== 0) {
$base=dirname($_SERVER['SCRIPT_FILENAME']);
$path=$base."/".$path;
}
// canonicalize
$path=explode('/', $path);
$newpath=array();
for ($i=0; $i<sizeof($path); $i++) {
if ($path[$i]==='' || $path[$i]==='.') continue;
if ($path[$i]==='..') {
array_pop($newpath);
continue;
}
array_push($newpath, $path[$i]);
}
$finalpath="/".implode('/', $newpath);
// check then return valid path or filename
if (file_exists($finalpath)) {
return ($finalpath);
}
else return FALSE;
}
?>