Home>Article>Backend Development> PHP file operation returns the location information of the file (absolute address)

PHP file operation returns the location information of the file (absolute address)

青灯夜游
青灯夜游 Original
2021-08-20 15:11:58 4884browse

In the previous article "How to quickly know the size, type and permissions of the file in php file operations", we introduced how to obtain the file size, file type and file/directory permissions. There are Friends who need it can learn about it~

The main content of this article is: return the location information of the current file, that is, obtain and return the absolute path of the current file.

So how to get the absolute path of the file? Here are some commonly used methods.

1. Use the realpath() function

We know the name of the executed PHP script, assign it to$filename, and then use realpath ($filename) to get the absolute path of the file (including the file name) and learn the location information of$filename.

The output result is:

PHP file operation returns the location information of the file (absolute address)

It can be seen that of course the file$filenameis in the sub-section of the wamp directory on the c drive In the directory www.

The absolute path returned by the realpath() function contains the file name. If you don’t want it, you can use the dirname() function to remove it.

PHP file operation returns the location information of the file (absolute address)

This is the known part File url to get the absolute path of the specified file; but in practice sometimes we don't know part of the url of the current file, so how to get the absolute path of the file? We can use magic constants (__FILE__,__DIR__) or predefined variables ($_SERVER)

2. Use magic constants__FILE__

Use the__FILE__constant directly to get the absolute path of the current file (including the file name)

The output result is:

PHP file operation returns the location information of the file (absolute address)

Same as above, if you don’t want the file name, you can use the dirname() function to remove it, that is, get the absolute directory information where the current file is located

PHP file operation returns the location information of the file (absolute address)

Calling the dirname() function twice can also obtain the upper directory name of the current file

The output result is:

当前文件所在位置: C:\wamp

3. Use magic constants__DIR__

Use the__DIR__constant directly to get the absolute path of the current file, and it does not include the file name. , equivalent todirname(__FILE__).

The output result is:

当前文件所在位置: C:\wamp\www

4. Use predefined variables$_SERVER

$_SERVERcontains information about the server and execution environment. It is an array containing information such as header, path, and script locations. All items in this array are created by the web server.

We can directly use$_SERVER['SCRIPT_FILENAME']to get the absolute path of the currently executing script

The output result is:

当前文件所在位置: C:/wamp/www/demo.php

PHP The Chinese website platform has a lot of video teaching resources. Everyone is welcome to learn "PHP Video Tutorial"!

The above is the detailed content of PHP file operation returns the location information of the file (absolute address). For more information, please follow other related articles on the PHP Chinese website!

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