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

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

青灯夜游
Release: 2023-03-12 07:40:01
Original
5056 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.

<?php
$filename = "demo.php";
echo "{$filename} 文件所在位置: ".realpath($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 $filename is 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
$filename = "demo.php";
echo "{$filename} 文件所在位置: ".dirname(realpath($filename));
?>
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)

<?php
echo "当前文件所在位置: ".__FILE__;
?>
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

<?php
echo "当前文件所在位置: ".dirname(__FILE__);
?>
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

<?php
header("content-type:text/html;charset=utf-8");
echo "当前文件所在位置: ".dirname(dirname(__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 to dirname(__FILE__).

<?php
echo "当前文件所在位置: ".__DIR__;
?>
Copy after login

The output result is:

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

4. Use predefined variables $_SERVER

$_SERVER contains 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

<?php
echo "当前文件所在位置: ".$_SERVER[&#39;SCRIPT_FILENAME&#39;];
?>
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template