path_is_absolute() WordPress Function

This function checks whether a given path is absolute. It is useful when you need to know if a given path is absolute or relative. For example, if you are including a file, you need to know if the path is absolute or relative so that you can properly include the file. This function returns true if the path is absolute, and false if the path is relative.

path_is_absolute( string $path ) #

Test if a given filesystem path is absolute.


Description

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


Top ↑

Parameters

$path

(string)(Required)File path.


Top ↑

Return

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


Top ↑

Source

File: wp-includes/functions.php

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] );
}


Top ↑

Changelog

Changelog
VersionDescription
2.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More