path_is_absolute( string $path ): bool

Tests if a given filesystem path is absolute.

Description

For example, ‘/foo/bar’, or ‘c:\windows’.

Parameters

$pathstringrequired
File path.

Return

bool True if path is absolute, false is not absolute.

Source

function path_is_absolute( $path ) {	/* * Check to see if the path is a stream and check to see if its an actual * path or file as realpath() does not support stream wrappers. */	if ( wp_is_stream( $path ) && ( is_dir( $path ) || is_file( $path ) ) ) {	return true;	}	/* * This is definitive if true but fails if $path does not exist or contains * a symbolic link. */	if ( realpath( $path ) === $path ) {	return true;	}	if ( strlen( $path ) === 0 || '.' === $path[0] ) {	return false;	}	// Windows allows absolute paths like this.	if ( preg_match( '#^[a-zA-Z]:\\\\#', $path ) ) {	return true;	}	// A path starting with / or \ is absolute; anything else is relative.	return ( '/' === $path[0] || '\\' === $path[0] ); } 

Changelog

VersionDescription
2.5.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    This note was added while current WordPress version is 5.7

    Please be aware that path_is_absolute() returns false on a Windows platform when used on a normalized (abs) path (trac #48289).

    var_dump( path_is_absolute( __FILE__ ) ); var_dump( path_is_absolute( wp_normalize_path( __FILE__ ) ) );

    *nix Result:

    bool(true)
    bool(true)

    Windows Result:

    bool(true)
    bool(false)

You must log in before being able to contribute a note or feedback.