public static function fullPath($path, $cwd = NULL, $allow_parent = FALSE) {
        //unix style full paths
        if(substr($path, 0, 1) == DIRECTORY_SEPARATOR) {
            return $path;
        }

        //windows style full paths
        if(substr($path, 1, 2) == ":\\") {
            return $path;
        }

        //throw exception if there are parent paths
        if(!$allow_parent) {
            $parts = explode(DIRECTORY_SEPARATOR, $path);
            if(array_search("..", $parts) !== FALSE) {
                throw new Exception("Parent directory in path '$path' detected. This is currently not allowed.");
            }
        }

        //get paths (with unresolved parent paths)
        $path = rtrim($cwd ?: getcwd(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path;

        //adjust windows paths for further processing
        if(substr($path, 1, 2) == ":\\") {
            $prefix = substr($path, 0, 3);
            $path = substr($path, 3);
        }
        else {
            $prefix = "";
        }

        //get parts
        $parts = explode(DIRECTORY_SEPARATOR, $path);

        //remove parts in front of .. parts
        $result = [];

        while($parts) {
            $part = array_shift($parts);

            if($part == "..") {
                if(count($result) <= 1) {
                    throw new Exception("'$path' can not exist.");
                }

                array_pop($result);
            }
            else {
                array_push($result, $part);
            }
        }

        //return new assembled path
        return $prefix.implode(DIRECTORY_SEPARATOR, $result);
    }
public static function fullPath($path, $cwd = NULL, $allow_parent = FALSE) {
        //unix style full paths
        if(substr($path, 0, 1) == DIRECTORY_SEPARATOR) {
            return $path;
        }

        //windows style full paths
        if(substr($path, 1, 2) == ":\\") {
            return $path;
        }

        //throw exception if there are parent paths
        if(!$allow_parent) {
            $parts = explode(DIRECTORY_SEPARATOR, $path);
            if(array_search("..", $parts) !== FALSE) {
                throw new Exception("Parent directory in path '$path' detected. This is currently not allowed.");
            }
        }

        //get paths (with unresolved parent paths)
        $path = rtrim($cwd ?: getcwd(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path;

        //adjust windows paths for further processing
        if(substr($path, 1, 2) == ":\\") {
            $prefix = substr($path, 0, 3);
            $path = substr($path, 3);
        }
        else {
            $prefix = "";
        }

        //get parts
        $parts = explode(DIRECTORY_SEPARATOR, $path);

        //remove parts in front of .. parts
        $result = [];

        while($parts) {
            $part = array_shift($parts);

            if($part == "..") {
                if(count($result) <= 1) {
                    throw new Exception("'$path' can not exist.");
                }

                array_pop($result);
            }
            else {
                array_push($result, $part);
            }
        }

        //return new assembled path
        return $prefix.implode(DIRECTORY_SEPARATOR, $result);
    }
public static function fullPath ( $ path , $ cwd = NULL , $ allow_parent = FALSE ) { //unix style full paths if ( substr ( $ path , 0 , 1 ) == DIRECTORY_SEPARATOR ) { return $ path ; } //windows style full paths if ( substr ( $ path , 1 , 2 ) == ":\\" ) { return $ path ; } //throw exception if there are parent paths if ( ! $ allow_parent ) { $ parts = explode ( DIRECTORY_SEPARATOR , $ path ) ; if ( array_search ( ".." , $ parts ) !== FALSE ) { throw new Exception ( "Parent directory in path '$path' detected. This is currently not allowed." ) ; } } //get paths (with unresolved parent paths) $ path = rtrim ( $ cwd ? : getcwd ( ) , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . $ path ; //adjust windows paths for further processing if ( substr ( $ path , 1 , 2 ) == ":\\" ) { $ prefix = substr ( $ path , 0 , 3 ) ; $ path = substr ( $ path , 3 ) ; } else { $ prefix = "" ; } //get parts $ parts = explode ( DIRECTORY_SEPARATOR , $ path ) ; //remove parts in front of .. parts $ result = [ ] ; while ( $ parts ) { $ part = array_shift ( $ parts ) ; if ( $ part == ".." ) { if ( count ( $ result ) <= 1 ) { throw new Exception ( "'$path' can not exist." ) ; } array_pop ( $ result ) ; } else { array_push ( $ result , $ part ) ; } } //return new assembled path return $ prefix . implode ( DIRECTORY_SEPARATOR , $ result ) ; }
Gets the full path from relative paths

This function will leave already full paths as they are.
Parent paths in $path are allowed if $allow_parent is set to true. Otherwise an exception is thrown.
If an impossible parent path was passed, an exception will also be thrown.

@param string $path The input path
@param string|NULL $cwd If path is a relative path, treat it as if is relative to this path. Defaults to current working directory.
@param bool $allow_upper Allow parent directories in $path (for example: ../../test/). If not allowed but detected, an exception will be thrown.
@return string The input path if it was a full path or a full path based on $cwd + $path otherwise.
Gets the full path from relative paths