PHP realpath

WBOY
發布: 2024-08-29 13:03:15
原創
342 人瀏覽過

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);
登入後複製

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:

"); ?>
登入後複製

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:

"); // printing the absolute path for "../" path print( "The absolute path for '../' path or after '../' path is : " ); print( $abs_path ); print( "
"); ?>
登入後複製

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:\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:

" ); // 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:

PHP realpath

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.

以上是PHP realpath的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!