PHP realpath() Function: Description, Parameters, Return Value, and Examples
This article explains PHP's realpath() function, detailing its purpose of returning a normalized absolute pathname, describing its parameters and return values, and providing two practical code examples that demonstrate path resolution on Unix and Windows systems.
realpath() returns a normalized absolute pathname.
string realpath(string $path)
The function resolves all symbolic links, removes "./", "../" and redundant slashes, and returns the canonical absolute path without any symbolic components.
Parameters
path : The path to be examined.
Return value
On success, the normalized absolute path is returned; on failure (e.g., the file does not exist) FALSE is returned.
Example 1 (Unix)
<?php chdir('/var/www/'); echo realpath('./../../etc/passwd'); ?>
Output:
/etc/passwd
Example 2 (Windows)
<?php echo realpath('/windows/system32'); ?>
Output:
C:\WINDOWS\System32
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.