PHP | realpath( ) Function

The realpath() function in PHP is an inbuilt function which is used to return the canonicalized absolute pathname.
The realpath() function removes all symbolic links such as ‘/./’ ‘/../’ and extra ‘/’ and returns the absolute pathname.
The path is sent as a parameter to the realpath() function and it returns the absolute pathname on success and a False on failure.

Syntax:

realpath(path)

Parameters Used:
The realpath() function in PHP accepts only one parameter.



  • path : It is a mandatory parameter which specifies the symbolic path whose absolute path the user wants to know.

Return Value:
It returns the absolute pathname on success and a False on failure.

Errors And Exception

  1. The realpath() function returns False if the running script does not have executable permissions on all directories in the hierarchy.
  2. The function realpath() will not work for a file which is inside a Phar as such a path is not a real path.
  3. Some filesystem functions may return unexpected results for files which are larger than 2GB since PHP’s integer type is signed and many platforms use 32bit integers.

Examples:

Input : echo realpath("gfg.txt");
Output : C:\xampp\htdocs\filehandling\gfg.txt

Input : chdir('/docs/assignment/');
        echo realpath('./../../gfg/articles');

Output : /gfg/articles

Below programs illustrate the realpath() function.

Suppose there is a file named “gfg.txt”

Program 1

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// returning absolute path 
// using realpath() function
echo realpath("gfg.txt");
  
?>

chevron_right


Output:

C:\xampp\htdocs\filehandling\gfg.txt

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// using chdir() to change directory
chdir('/docs/assignment/');
  
// returning absolute path using realpath() function
echo realpath('./../../gfg/articles');
  
?>

chevron_right


Output:

/gfg/articles

Reference:
http://php.net/manual/en/function.realpath.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.