PHP realpath

WBOY
Release: 2024-08-29 13:03:15
Original
341 people have browsed it

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

realpath(path);
Copy after login

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:xampphtdocsprograms” 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>");
?>
Copy after login

Output:

PHP realpath

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>");
?>
Copy after login

Output:

PHP realpath

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:xampphtdocsprograms”, next the path run is “../” which means go back to the previous directory, so now the path is “C:xampphtdocs”, 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);
?>
Copy after login

Output:

PHP realpath

As in the above code, the change the current directory (as “C:xampphtdocsprograms”) to “xampphtdocs” 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:xampphtdocsprograms” which is not contain any symbolic link( as ‘./’ ). Similarly, for the path “./././programs” also return the absolute path from the ‘C’ drive as “C:xampphtdocsprograms”, 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.

The above is the detailed content of PHP realpath. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!