


init.php file PHP summary of methods to obtain the parent directory of the current file
Method 1: First get the length of the folder where the current file is located, and then use substr to intercept the length:
Copy the code The code is as follows:
$dirName = str_replace("\", "/", dirname (__FILE__));
$dirNameLength = strlen($dirName);
$currentDirNameLength = $dirNameLength - strrpos($dirName,"/"); //Get the length of the folder where the current file is located!
$parentDirName = substr($dirName,0,-$currentDirNameLength); //If the third parameter is a negative number, it means that [the absolute value of the parameter] characters will be intercepted from the back.
Method 2: Treat the folder where the current file is located as a file (the folder is actually a special file, everything is a file!!), just use dirname to nest the dirname once:
$parentDirName = dirname(dirname(__FILE__));
From It can be seen from the above: a deep understanding of things can greatly improve the quality of the code!
Attached: PHP gets the path or directory implementation
PHP gets the directory and the method through magic variables; through super global variables; through related functions, etc.:
<?php /** * PHP获取路径或目录实现 */ //魔术变量,获取当前文件的绝对路径 echo "__FILE__: ========> ".__FILE__; echo '<br/>'; //魔术变量,获取当前脚本的目录 echo "__DIR__: ========> ".__DIR__; echo '<br/>'; //dirname返回路径的目录部分,dirname(__FILE__)相当于__DIR__ echo "dirname(__FILE__): ========> ".dirname(__FILE__); echo '<br/>'; //$_SERVER['PHP_SELF']和$_SERVER['SCRIPT_NAME']的结果一般相同,他们都是获取当前脚本的文件名 //只有当php以cgi方式运行时有区别,但是现在几乎找不到以cgi方式运行php了 echo '$_SERVER["PHP_SELF"]: ========> '.$_SERVER['PHP_SELF']; echo '<br/>'; echo '$_SERVER["SCRIPT_NAME"]: ========> '.$_SERVER['SCRIPT_NAME']; echo '<br/>'; //当前执行脚本的绝对路径。记住,在CLI方式运行php是获取不到的 echo '$_SERVER["SCRIPT_FILENAME"]: ========> '.$_SERVER['SCRIPT_FILENAME']; echo '<br/>'; //当前运行脚本所在的文档根目录。在服务器配置文件中定义。 echo '$_SERVER["DOCUMENT_ROOT"]: ========> '.$_SERVER['DOCUMENT_ROOT']; echo '<br>'; //getcwd()返回当前工作目录 echo "getcwd(): ========> ".getcwd(); echo '<br>';
The above introduces the init.php file and a summary of PHP methods to obtain the parent directory of the current file, including the content of the init.php file. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
