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

Karen Carpenter
Release: 2023-03-12 07:40:01
Original
4891 people have browsed it

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.

Copy after login

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.

Copy after login

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)

Copy after login

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

Copy after login

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

Copy after login

The output result is:

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

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__).

Copy after login

The output result is:

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

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

Copy after login

The output result is:

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

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!

Related labels:
source:php.cn
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
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!