note 84012 added to function.realpath

From: Date: Mon, 23 Jun 2008 22:43:59 +0000
Subject: note 84012 added to function.realpath
Groups: php.notes 
Request: Send a blank email to php-notes+get-140754@lists.php.net to get a copy of this message
Because realpath() does not work on files that do not exist, I wrote a function that does. It replaces (consecutive) occurences of / and \\ with whatever is in DIRECTORY_SEPARATOR, and processes /. and /.. fine. Paths returned by get_absolute_path() contain no (back)slash at position 0 (beginning of the string) or position -1 (ending) <?php function get_absolute_path($path) { $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); $absolutes = array(); foreach ($parts as $part) { if ('.' == $part) continue; if ('..' == $part) { array_pop($absolutes); } else { $absolutes[] = $part; } } return implode(DIRECTORY_SEPARATOR, $absolutes); } ?> A test: <?php var_dump(get_absolute_path('this/is/../a/./test/.///is')); ?> Returns: string(14) "this/a/test/is" As you can so, it also produces Yoda-speak. :) ---- Server IP: 193.190.67.11 Probable Submitter: 81.247.96.125 ---- Manual Page -- http://www.php.net/manual/en/function.realpath.php Edit -- https://master.php.net/note/edit/84012 Del: integrated -- https://master.php.net/note/delete/84012/integrated Del: useless -- https://master.php.net/note/delete/84012/useless Del: bad code -- https://master.php.net/note/delete/84012/bad+code Del: spam -- https://master.php.net/note/delete/84012/spam Del: non-english -- https://master.php.net/note/delete/84012/non-english Del: in docs -- https://master.php.net/note/delete/84012/in+docs Del: other reasons-- https://master.php.net/note/delete/84012 Reject -- https://master.php.net/note/reject/84012 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#140754) next »