Table of Contents
1 Absolute path, relative path and undetermined path
2. Relative path:
3. Absolute path
4. Undetermined path
Test environment description
5. Solution
Home Backend Development PHP Problem What should I do if the relative directory in php cannot be opened?

What should I do if the relative directory in php cannot be opened?

Dec 01, 2022 am 09:25 AM
php relative directory

Solution to the problem that php cannot be opened in a relative directory: 1. Use "absolute path"; 2. Define a general function import.php, set it to "automatically import files in advance", and add it in php.ini Just modify the content to "auto_prepend_file = "C:\xampp\htdocs\auto_prepend_file.php"".

What should I do if the relative directory in php cannot be opened?

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

php Relative directory cannot be opened? Summary of require and include path issues in PHP

1 Absolute path, relative path and undetermined path

Relative path

Relative path refers to the path starting with., For example

./a/a.php (相对当前目录)    
../common.inc.php (相对上级目录),
Copy after login

Absolute path

The absolute path is a path starting with / or a drive letter similar to C:/ under Windows. The full path can uniquely determine the final file without any reference path. address. For example

/apache/wwwroot/site/a/a.php
c:/wwwroot/site/a/a.php
Copy after login

Undetermined path

Any path that does not start with . or /, nor does it start with the Windows drive letter:/, for example

a/a.php  
common.inc.php,
Copy after login

I initially thought this was also a relative path Path, but in PHP's include/require inclusion mechanism, this type of path is completely different from the relative path processing starting with . require './a.php' and require 'a.php' are different!

Let’s analyze the processing methods of these three types of include paths: First, remember a conclusion: if the include path is a relative path or an absolute path, it will not go to include_path (include_path defined in php.ini Environment variables, or use set_include_path(...) in the program to find the file.

Test environment description

Note: The following discussion and conclusion are based on this environment: Assumption A=http://www.xxx.com/app/test/ a.php, again emphasize that the following discussion is for the case of direct access to A.

2. Relative path:

The relative path requires a reference directory to determine the final path of the file. In the include resolution, No matter how many levels of nesting are included, this reference directory is the directory where the program execution entry file is located .

Example 1

A中定义  require './b/b.php';  // 则B=[SITE]/app/test/b/b.php
B中定义  require './c.php';    // 则C=[SITE]/app/test/c.php 不是[SITE]/app/test/b/c.php
Copy after login

Example 2

A中定义  require './b/b.php';  // 则B=[SITE]/app/test/b/b.php 
B中定义  require '../c.php';   // 则C=[SITE]/app/c.php  不是 [SITE]/app/test/c.php
Copy after login

Example 3

A中定义  require '../b.php';   //则B=[SITE]/app/b.php 
B中定义  require '../c.php';   //则C=[SITE]/app/c.php  不是 [SITE]/c.php
Copy after login

Example 4:

A中定义  require '../b.php';   // 则B=[SITE]/app/b.php 
B中定义  require './c/c.php';  / /则C=[SITE]/app/test/c/c.php  不是 [SITE]/app/c/c.php
Copy after login

Example 5

A中定义  require '../inc/b.php';  // 则B=[SITE]/app/inc/b.php 
B中定义  require './c/c.php';     // 则C还是=[SITE]/app/test/c/c.php  不是 [SITE]/app/inc/c/c.php
Copy after login

Example 6

A中定义  require '../inc/b.php';  // 则B=[SITE]/app/inc/b.php 
B中定义  require './c.php';       // 则C=[SITE]/app/test/c.php  不是 [SITE]/app/inc/c.php
Copy after login

3. Absolute path

The absolute path is relatively simple and not easy to cause confusion and errors. require|inclue corresponds to the file on the disk.

require '/wwwroot/xxx.com/app/test/b.php';    // Linux中
require 'c:/wwwroot/xxx.com/app/test/b.php';  // windows中
Copy after login

dirname(__FILE__) is also calculated as a directory in the form of an absolute path, but it should be noted that __FILE__ is a Magic constants, and it is equivalent to writing this statement at any time. The absolute path where the php file is located, so dirname(__FILE__) always points to the absolute path of the php file where this statement is written, and has nothing to do with whether this file is included and used by other files.

Example 1

A中定义  require '../b.php';                  // 则B=[SITE]/app/b.php
B中定义  require dirname(__FILE__).'/c.php';  // 则B=[SITE]/app/c.php
Copy after login

Example 2

A中定义  require '../inc/b.php';              // 则B=[SITE]/app/inc/b.php
B中定义  require dirname(__FILE__).'/c.php';  // 则B=[SITE]/app/inc/c.php 始终跟B在同一个目录
Copy after login

Conclusion: No matter whether B is included and used by A, or directly accessed

B如果 require dirname(__FILE__).'/c.php';    // 则始终引用到跟B在同一个目录中的 c.php文件; 
B如果 require dirname(__FILE__).'/../c.php'; // 则始终引用到B文件所在目录的父目录中的 c.php文件; 
B如果 require dirname(__FILE__).'/c/c.php';  // 则始终引用到B文件所在目录的c子目录中的 c.php文件;
Copy after login

4. Undetermined path

First use the include directories defined in include_path to splice [undetermined path] one by one. If an existing file is found, the include will exit successfully. If not found, use the directory where the php file that executes the require statement is located to splice [ Undetermined path] to find the file. If the file exists, it will exit successfully. Otherwise, it means the file does not exist and an error will occur. Undetermined paths are easy to confuse and are not recommended.

5. Solution

Since the "reference directory" in the "relative path" is the directory where the execution entry file is located, "undetermined" The path is also easy to confuse, so the best solution is to use the "absolute path" ; For example, the content of b.php is as follows, no matter where b.php is required, the path of b.php will be used as a reference Come to require

$dir = dirname(__FILE__);
require($dir . '../c.php');
Copy after login

of c.php or define a general function import.php, set it to "automatically import files in advance", and make the following configuration in php.ini

更改配置项(必须)auto_prepend_file = "C:\xampp\htdocs\auto_prepend_file.php"
更改配置项(可选)allow_url_include = On
Copy after login

import.php The content is as follows

function import($path) {    
    $old_dir = getcwd();        // 保存原“参照目录”
    chdir(dirname(__FILE__));    // 将“参照目录”更改为当前脚本的绝对路径
    require_once($path);
    chdir($old_dir);            // 改回原“参照目录”
}
Copy after login

In this way, you can use the import() function to require the file. No matter how many levels of "reference directories" it contains, it is the current file

Recommended learning: "PHP Video Tutorial

The above is the detailed content of What should I do if the relative directory in php cannot be opened?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1258
29
C# Tutorial
1232
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

See all articles