A little function to readlink TO THE END:
(realpath can't do this if the symlink (ultimately) points to a non-existing path, since it just returns false in this case.)
function readlinkToEnd($linkFilename) {
if(!is_link($linkFilename)) return $linkFilename;
$final = $linkFilename;
while(true) {
$target = readlink($final);
if(substr($target, 0, 1)=='/') $final = $target;
else $final = dirname($final).'/'.$target;
if(substr($final, 0, 2)=='./') $final = substr($final, 2);
if(!is_link($final)) return $final;
}
}