views:

40

answers:

2

Apparently, realpath is very buggy. In PHP 5.3.1, it causes random crashes. In 5.3.0 and less, realpath randomly fails and returns false (for the same string of course), plus it always fails on realpath-ing the same string twice/more (and of course, it works the first time).

Also, it is so buggy in earlier PHP versions, that it is completely unusable. Well...it already is, since it's not consistent.

Anyhow, what options do I have? Maybe rewrite it by myself? Is this advisable?

A: 

I have never heard of such massive problems with realpath() (I always thought that it just interfaces some underlying OS functionality - would be interested in some links), but the User Contributed Notes to the manual page have a number of alternative implementations. Here is one that looks okay.

Of course, it's not guaranteed these implementations take care of all cross-platform quirks and issues, so you'd have to do thorough testing to see whether it suits your needs.

As far as I can see though, none of them returns a canonicalized path, they only resolve relative paths. If you need that, I'm not sure whether you can get around realpath() (except perhaps executing a (system-dependent) console command that gives you the full path.)

Pekka
Resolving symlinks was one of the main reasons of using realpath, sadly enough...
Christian Sciberras
+1  A: 

Thanks to Sven Arduwie's code (pointed out by Pekka) and some modification, I've built a (hopefully) better implementation:

/**
 * This function is to replace PHP's extremely buggy realpath().
 * @param string The original path, can be relative etc.
 * @return string The resolved path, it might not exist.
 */
function truepath($path){
    // attempts to detect if path is relative in which case, add cwd
    if(strpos($path,':')===false && (strlen($path)==0 || $path{0}!='/')){
        $path=getcwd().DIRECTORY_SEPARATOR.$path;
    }
    // resolve path parts (single dot, double dot and double delimiters)
    $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;
        }
    }
    $path=implode(DIRECTORY_SEPARATOR, $absolutes);
    // if file exists and it is a link, use readlink to resolves links
    if(file_exists($path) && linkinfo($path)>0)$path=readlink($path);
    return $path;
}

NB: Unlike PHP's realpath, this function does not return false on error; it returns a path which is as far as it could to resolving these quirks.

Christian Sciberras
Looks good. I tested it a little bit on Windows and it works fine, even across drive letters.
Pekka
Thanks a lot for the testing! Knowing it is very stable is of huge importance to my project.
Christian Sciberras