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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
Given below are the examples mentioned:
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:
"); ?>
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 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:
"); // printing the absolute path for "../" path print( "The absolute path for '../' path or after '../' path is : " ); print( $abs_path ); print( "
"); ?>
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 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:
" ); // 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( "
" ); $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 ‘./././’ ).
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.
以上是PHP realpath的詳細內容。更多資訊請關注PHP中文網其他相關文章!