Updated April 5, 2023
Introduction to PHP realpath
The PHP realpath() function is used to return the absolute pathname. The realpath() function is a built-in function in PHP. The realpath() function can remove all symbolic links likes ‘/./’, ‘/../’ and extra character ‘/’ and returns the absolute pathname excluding these symbols. The realpath() function accepts the path as a parameter and returns the absolute pathname for the passes path on success and returns false on failure.
Syntax:
realpath(path);
Parameter:
path: This is not an optional parameter string type that specifies the path with the symbolic links whose absolute path is to be returned. If a path is blank or Null, then the path is interpreted as the current directory.
Return Value: The return value of this method is absolute pathname without a symbolic link on the success and false on the failure.
Working of PHP realpath() Function
- It accepts the one parameter as (path), which is the required parameter.
- Suppose we have a path ‘./././programs’ for which we want to get the absolute path, so we need to pass this path by calling the function as realpath( ‘./././programs’ ), which return the absolute path as “C:\xampp\htdocs\programs” without any symbolic link.
Examples
Given below are the examples mentioned:
Example #1
Examples for the PHP realpath( ) function to get the absolute path of the file.
Next, we write the PHP code to see the PHP realpath() function more clearly with the following example, where the realpath( ) function is used to get the absolute path of the file.
Code:
<?php
// file to get its real path
$file_path = "Ex.txt";
// return an absolute path using realpath() function
$abs_path = realpath( $file_path );
// printing the absolute path of the file
print( "The absolute path of the file is : " );
print( $abs_path );
print( "<br>");
?>
Output:
As in the above code, the absolute pathname is generating with the help of the realpath() function as “realpath( $file_path );” where the $file_path variable contains the name of the file which is present in the same directory as the running program file is present.
Example #2
Example for the PHP realpath() function to get absolute for the given path.
Next, we write the PHP code to see the function more clearly with the following example, where the realpath( ) function is used to get the absolute path for the given path, which contain the symbolic link.
Code:
<?php
// file to get its real path
$path = "../";
// return an absolute path
// of the current directory
// using realpath() function with NULL
$curr_path = realpath( NULL );
// return an absolute path after "../" path using realpath() function
$abs_path = realpath( $path );
// printing the absolute path of current directory
print( "The absolute path of the current directory is : " );
print( $curr_path );
print( "<br>");
// printing the absolute path for "../" path
print( "The absolute path for '../' path or after '../' path is : " );
print( $abs_path );
print( "<br>");
?>
Output:
As in the above code, the current absolute pathname is printing with the help of realpath( ) function as “realpath( NULL );” if the value is NULL, then the realpath( ) function returns the absolute path of the current directory, which is “C:\xampp\htdocs\programs”, next the path run is “../” which means go back to the previous directory, so now the path is “C:\xampp\htdocs”, as we can see in the output.
Example #3
Example for the PHP realpath() function to get absolute for the given path with change directory.
Next, we write the PHP code to see the function more clearly with the following example, where the realpath() function is used to get the absolute path for the given path, which contain the symbolic link with the change directory.
Code:
<?php
// change directory to /xampp/htdocs/
chdir( '/xampp/htdocs/' );
// move to next dirctory programs
// with './' symbolic linnk
$chdr = realpath( './programs' );
// Now printing the absolute path after './programs'
print( "The absolute path for './programs' path is : " );
print( $chdr );
print( "<br>" );
// move to back previous dirctory
// with './././programs' symbolic linnk and directory name
$path = realpath( './././programs' ) ;
print( "The absolute path for './././programs' path is : " );
print( $path );
print( "<br>" );
$curr_path = realpath( NULL );
print( "The absolute path for current path is : " );
print($curr_path);
?>
Output:
As in the above code, the change the current directory (as “C:\xampp\htdocs\programs”) to “\xampp\htdocs\” by using the chdir() function. Next, the path contains a symbolic link as “./programs”, which is given to the realpath() function, so the function returns its absolute path as “C:\xampp\htdocs\programs” which is not contain any symbolic link( as ‘./’ ). Similarly, for the path “./././programs” also return the absolute path from the ‘C’ drive as “C:\xampp\htdocs\programs”, which is not contain any symbolic link( as ‘./././’ ).
Conclusion
It is a built-in function in PHP, which is used to get the absolute pathname that does not contain any symbolic links for the given path, containing the symbolic links.
Recommended Articles
This is a guide to PHP realpath. Here we discuss the introduction, working of the PHP realpath( ) function, and examples, respectively. You may also have a look at the following articles to learn more –