Home Backend Development PHP Tutorial Analysis of the relationship between php absolute paths and relative paths_PHP tutorial

Analysis of the relationship between php absolute paths and relative paths_PHP tutorial

Jul 21, 2016 pm 03:40 PM
asp php cannot and relation analyze root use relatively absolute express path

php中好像不能像asp那样用“/”表示根目录,代之以$_SERVER['DOCUMENT_ROOT'],其它则相同:../表示向上一层。./表示当前层。假如现在a/b/c/s.php要调用根目录下的 /bb/s2.txt,则:

$RootDir = $_SERVER['DOCUMENT_ROOT'];
$fireDir = "$RootDir/bb/s2.txt";

或者:“../../../bb/s2.txt”表示向上返回到b再向上到a再向上到根目录然后到bb下。

前一阵子老是受php开发中,文件互相引入require()相对位置关系的困扰,为了彻底弄清它们的关系,笔者做了个实验。

以下是实验图:

当前项目(project2)的绝对路径是:D:\www\php_case\Coucom_make。也就是我们当前项目的根目录root.
  
为了能更加清楚的表述不同级别的目录文件相互的引入问题,偶大胆将引用分成三种类型即:上级对下级的引用(简称上级引用,英文译为:superior to underling。简称(stou)).

下级对上级的引用(简称下级引用,英文反之便是)

平级引用或叫同级引用(英文:paratactic)。  
 
好了,我们明确了引用类型,下面我们来看不同类型引用它们有着什么样的规则。

我们先来说说上级引用:

看我们的实验图,在图中项目下分别有aa bb ee 三个同级目录和一个index.php文件,在bb下又有cc目录,cc下又有dd目录和cc.php ccc.php两个文件,同样dd下也包含一个dd.php文件。凡是上层对下层均属上级引用.

例如:index.php对于所有文件的引用:
   cc .php对dd.php的引用:
   ee.php对dd.php的引用:

大家仔细看一下目录结构,便会发现这三种引用虽然同属于上级引用,但它们又不完全相同,我把它分成两种情况:即在上级引用中存在两种引用情况:1.从属的上级引用(类如cc.php对dd.php,,因为这两个文件同属于cc目录)2.非从属的上级引用(类如index.php对于所有文件的引用和ee.php对dd.php的引用都属于这种情况因为它们与被引用的文件并没有一个共同的父目录,在能相对于站点根目录).

对于从属上级引用:

以下是在cc.php对dd.php的引用

require('dd/dd.php');

对于非从属上级引用:

以下是在ee.php中对cc.php的引用

require('../bb/cc/cc.php');

The above mentioned are upper-level quotes, let’s learn about lower-level quotes below! In the same way, references from lower layers to upper layers are subordinate references, which are also divided into subordinate and non-subordinate categories. The relative paths of subordinate subordinate references take their parent directory as the root directory, for example:

Ttt.php’s reference to bbff.php falls into this situation: require('../bb/cc/cc.php');

Non-affiliated subordinate references are based on the root directory of the website, for example:

ccc.php’s reference to ee.php: require('../../ee/ee.php');

The above is an introduction to subordinate references. Finally, let’s take a look at parallel references, or peer references. In fact, peer references are also divided into two situations: subordinate peer references and non-subordinate peer references

Dependent sibling references are simple: references to two files in the same directory

For example: refer to ttt.php in dd.php require('ttt.php');

Non-subordinate peer reference: that is, a reference to two files that are not in the same directory (no common parent directory, only the website directory is the parent directory), but have the same level, for example: in aa. php is referencing ee.php require('../ee/ee.php');

The above are the references in three types of situations, and there are also issues involving nested references

For example:

ff.php refers to dd.php, and dd.php refers to gf.php. In this case, originally dd.php referencing gf.php is a non-subordinate reference in the subordinate reference. The writing method is like this: require( '../../../ee/gf.php'); and ff.php's reference to dd.php is a subordinate reference in the superior reference. It is written like this: require('./cc/dd/dd. php'); But you will find that the gf.php file cannot be found in ff.php, so how to write it? I tell you that you should write this in dd.php: require('../ee/gf.php'); This is the only correct way to write it. Why? Because when it comes to nested references, the relative path of the referenced file must be based on the final referenced file!

In short:

In fact, it’s very simple. You use your web root directory as the root directory. No matter how you decide, your files must have a file that must be included in all files. For example, some are global.php, and some are common. php

If this file is placed in the root directory

wwwroot/global.php

In the first line, add chdir(dirname(__FILE__)); //Switch to the directory where global.php is located and follow the directory

Use other files

require "../../../global.php";
require "aa/aa.php";
require "bb/bb/cc.php";

That’s it, because your global.php has already switched the path to wwwroot, so you don’t have to go through as much trouble as many people

define('ROOT_PATH',dirname(__FILE__));
require ROOT_PATH.....

chdir(dirname(__FILE__)); is indeed very easy to use. When importing this file at a relative position, other imported files only need to be based on it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321391.htmlTechArticleIt seems that in php, you cannot use "/" to represent the root directory like asp, instead use $_SERVER['DOCUMENT_ROOT' ], the others are the same: ../ means going up one level. ./ represents the current layer. If a/b/c/s.php now wants...
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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
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 vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

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 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.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles