GoLang realpath

is this article helpful? yes | no
GoLang replacement for PHP's realpath [edit | history]
func Realpath(path string) (string, error) {
	return filepath.Abs(path)
}

PHP realpath

PHP original manual for realpath [ show | php.net ]

realpath

(PHP 4, PHP 5, PHP 7)

realpathReturns canonicalized absolute pathname

Description

string realpath ( string $path )

realpath() expands all symbolic links and resolves references to /./, /../ and extra / characters in the input path and returns the canonicalized absolute pathname.

Parameters

path

The path being checked.

Note:

Whilst a path must be supplied, the value can be blank or NULL In these cases, the value is interpreted as the current directory.

Return Values

Returns the canonicalized absolute pathname on success. The resulting path will have no symbolic link, /./ or /../ components. Trailing delimiters, such as \ and /, are also removed.

realpath() returns FALSE on failure, e.g. if the file does not exist.

Note:

The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return FALSE.

Note:

For case-insensitive filesystems realpath() may or may not normalize the character case.

Note:

The function realpath() will not work for a file which is inside a Phar as such path would be a virual path, not a real one.

Note: Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB.

Changelog

Version Description
5.3.0 Prior to this release, if only the last path component did not exist, realpath() would not fail on *BSD systems. realpath() now fails in this case.
5.2.1 Prior to this version, realpath() returned FALSE if path is an empty string or NULL.

Examples

Example #1 realpath() example

<?php
chdir
('/var/www/');
echo 
realpath('./../../etc/passwd') . PHP_EOL;

echo 
realpath('/tmp/') . PHP_EOL;
?>

The above example will output:

/etc/passwd
/tmp

Example #2 realpath() on Windows

On windows realpath() will change unix style paths to windows style.

<?php
echo realpath('/windows/system32'), PHP_EOL;

echo 
realpath('C:\Program Files\\'), PHP_EOL;
?>

The above example will output:

C:\WINDOWS\System32
C:\Program Files

See Also

  • basename() - Returns trailing name component of path
  • dirname() - Returns a parent directory's path
  • pathinfo() - Returns information about a file path