


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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment