


PHP file operation returns the location information of the file (absolute address)
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); ?>
The output result is:
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)); ?>
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__; ?>
The output result is:
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__); ?>
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__)); ?>
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 to dirname(__FILE__)
.
<?php echo "当前文件所在位置: ".__DIR__; ?>
The output result is:
当前文件所在位置: C:\wamp\www
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['SCRIPT_FILENAME']; ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
