note 75992 modified in function.realpath by danbrown

From: Date: Mon, 01 Jun 2009 13:01:11 +0000
Subject: note 75992 modified in function.realpath by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156233@lists.php.net to get a copy of this message
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; } ?> --was-- 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). 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; } http://php.net/manual/en/function.realpath.php

« previous php.notes (#156233) next »