search
Homephp教程php手册PHP中你应该知道的require()文件包含的正确用法,

PHP中你应该知道的require()文件包含的正确用法,

以前看一些PHP框架源码的时候,很奇怪在文件包含的时候,会用dirname(__FILE__)来拼凑文件路径,不知道这样做有什么好处,后来终于发现了其中的缘由。

我们来看一个简单的例子:

有a,b,c三个php文件。a.php在网站根目录,b.php在b文件夹下——b/b.php,c.php在c文件夹下——c/c.php。有些混乱?看图就一目了然了:

a.php 和 b.php 都包含了 c.php,最后 c.php 包含了d文件夹下的一个php文件——d/d.php。

我们先来看a.php:

<&#63;php 
  $file_name = 'a.php';
  echo "this is a.php";
  echo "<hr>";
  require('c/c.php');
 &#63;>

很简单的代码,打印输出后,包含了c/c.php,接着,我们需要看c/c.php:

<&#63;php 
  $c_file_name = 'c.php';
  echo 'this is c.php, is required by ' . $file_name;
  echo "<hr>";
  require('../d/d.php');
 &#63;>

打印输出 "this is c.php, is required by a.php",$file_name是在a.php中定义的变量。在最后,包含了d.php。因为d文件夹在当前c.php文件的上一层,所以,按照常理,我们会理所当然的把路径写成 "../d/d.php"。但是很遗憾,会报错。原因在于,在被包含的文件中如c.php,再去包含其他文件,路径是相对于最外层的父文件来说的,也就是相对于a.php,可以理解为因为你被我包含了,所以你要以我为准。看起来很玄乎,原理其实很简单:你可以把 require('c/c.php'); 看成是c/c.php文件里的代码,这样我们的a.php看起来可以是这个样子:

<&#63;php 
  $file_name = 'a.php';
  echo "this is a.php";
  echo "<hr>";
  // require('c/c.php');
  $c_file_name = 'c.php';
  echo 'this is c.php, is required by ' . $file_name;
  echo "<hr>";
  require('../d/d.php');
 &#63;>

到此,你可以看到,我们要包含d/d.php文件时,刚才的路径是不是错误的了?因为,现在是在a.php的代码里,我们是相对于a.php文件来说的,当然,路径应该是 require('d/d.php'); 才对了。我们修改代码如下:

<&#63;php 
  $file_name = 'a.php';

  echo "this is a.php";
  echo "<hr>";

  // require('c/c.php');
  $c_file_name = 'c.php';

  echo 'this is c.php, is required by ' . $file_name;
  echo "<hr>";

  require('d/d.php');
 &#63;>

此时,你还没有领悟到深意,需要往下看,我们再看b/b.php:

<&#63;php 
  $file_name = 'b.php';
  echo "this is b.php";
  echo "<hr>";
  
  require('../c/c.php');
 &#63;>

不需要解释了吧,没啥问题,但是当你把 require('../c/c.php'); 换成 c/c.php 里面的代码的时候,你就会发现问题了,注意,我们刚才修改了c/c.php里的代码,把 require('../d/d.php'); 改成了 require('d/d.php'); 看下面包含进来后的代码:

<&#63;php 
  $file_name = 'b.php';
  echo "this is b.php";
  echo "<hr>";
  
  // require('../c/c.php');
  $c_file_name = 'c.php';
  echo 'this is c.php, is required by ' . $file_name;
  echo "<hr>";
  require('d/d.php');
 &#63;>

那么,相对于 b/b.php 来说,require('d/d.php'); 的路径错了,应该是 require('../d/d.php'); 才对。你回去修改 c/c.php 中的require路径,但是不对呀,你改了之后,b/b.php可以正常运行了,但是 a/a.php 又不行了,是不是,它们共用 c/c.php ,牵一发动全身,怎么办呢。

这个时候,我们回到文章开头提到的 dirname(__FILE__),这可是个好东西,可以完全解决以上问题。用了它,就可以不用关心包含你的文件是哪个文件、在哪个路径下面了,不需要顾虑父文件所在的层级,因为,dirname(__FILE__)可以相对于当前文件指定路径。也就是说,我们需要将我们的 c/c.php 中的 require 路径换为:

<&#63;php 
  $c_file_name = 'c.php';

  echo 'this is c.php, is required by ' . $file_name;
  echo "<hr>";

  require(dirname(__FILE__) . '/../d/d.php');
 &#63;>

这里,我们只需要把 c/c.php 作为参照,相对于它来说,d/d.php 在上一层。这样,就只有一个标准了,那就是,以我为准,管你包含我,还是他包含我,我只以我自己为准,我要包含的文件只相对于我自己而言了。

对于 dirname(__FILE__) 不明白的同修,请google,很简单。

好了,PHP技术分享到此结束,有任何疑问或有错误之处,请留言。话说,这是我的第一个标准技术博文。第一篇是水文,第二篇是准技术,今天终于写了篇技术的,欧也。

Statement
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
vue3+vite:src使用require动态导入图片报错怎么解决vue3+vite:src使用require动态导入图片报错怎么解决May 21, 2023 pm 03:16 PM

vue3+vite:src使用require动态导入图片报错和解决方法vue3+vite动态的导入多张图片vue3如果使用的是typescript开发,就会出现require引入图片报错,requireisnotdefined不能像使用vue2这样imgUrl:require(&rsquo;&hellip;/assets/test.png&rsquo;)导入,是因为typescript不支持require所以用import导入,下面介绍如何解决:使用awaitimport

require的用法有哪些require的用法有哪些Nov 27, 2023 am 10:03 AM

require用法:1、引入模块:在许多编程语言中,require用于引入外部模块或库,以便在程序中使用它们提供的功能。例如,在Ruby中,可以使用require来加载第三方库或模块;2、导入类或方法:在一些编程语言中,require用于导入特定的类或方法,以便在当前文件中使用它们;3、执行特定任务:在一些编程语言或框架中,require用于执行特定的任务或功能。

解决php标题中的fatal error: require(): Failed opening required 'data/tdk.php' (include_path='.;C:\php\pear')的步骤解决php标题中的fatal error: require(): Failed opening required 'data/tdk.php' (include_path='.;C:\php\pear')的步骤Nov 27, 2023 pm 12:51 PM

解决PHP标题中的fatalerror:require():Failedopeningrequired'data/tdk.php'(include_path='.;C:phppear')的步骤在使用PHP开发网站或应用程序时,我们经常会遇到各种错误。其中一个常见的错误是"fatalerror:require():Failed

解决php标题中的fatal error: require(): Failed opening required 'data/tdk.php'的步骤解决php标题中的fatal error: require(): Failed opening required 'data/tdk.php'的步骤Nov 27, 2023 am 10:41 AM

解决PHP标题中的FatalError:require():Failedopeningrequired'data/tdk.php'的步骤在开发和维护PHP网站时,我们经常会遇到各种错误和异常。其中一个常见的错误是"FatalError:require():Failedopeningrequired'data/tdk.php'"。

C预处理器?C预处理器?Aug 27, 2023 pm 02:53 PM

C编程语言支持预处理器,以便有效地工作程序。C预处理器是基于C的编程语言的宏预处理器。预处理器以明确的方式为编译器提供了包括头文件、宏展开、条件编译和行控制的能力。#hash标签用于定义预处理器,即所有预处理器都以#开头。其后是预处理器的名称,之间没有任何空格。这里是C预处理器的列表。S.No.预处理器描述1.#include包括来自文件的特定头文件。2.#define定义预处理器宏。3.#undef取消定义预处理器宏4.#if检查编译时条件并评估为True值。5.#else作为if预处理器的替

PHP语言开发中如何避免文件包含漏洞攻击?PHP语言开发中如何避免文件包含漏洞攻击?Jun 09, 2023 pm 06:55 PM

随着互联网的发展和普及,Web应用程序的安全性成为众多开发者关注的焦点。当涉及到Web应用程序的安全,文件包含漏洞攻击是一个非常重要的安全问题。该漏洞允许攻击者操纵访问应用程序的文件,这会导致重大问题,例如运行恶意代码或修改数据等行为。在PHP语言开发中,如何避免文件包含漏洞攻击呢?以下是一些常见的方法:1.设置文件路径限制可以通过设置文件路径的限制来避免文

PHP中require关键字的作用和使用方法详解PHP中require关键字的作用和使用方法详解Jun 28, 2023 pm 11:31 PM

PHP中require关键字的作用和使用方法详解在PHP开发中,require是一个非常常用的关键字。它的作用是将指定的文件包含进来,以供当前脚本使用。本文将详细讲解require关键字的作用和使用方法。一、require关键字的作用require关键字可以将一个文件的内容包含到当前脚本中。它通常用于包含一些必要的外部文件,比如库文件、配置文件等。使用req

如何在一个php.ini文件中包含另一个php.ini文件?如何在一个php.ini文件中包含另一个php.ini文件?Sep 02, 2023 pm 03:45 PM

无法包含主php,ini文件中的.ini文件。相反,在编译PHP时,该行--with-config-file-scan-dir=PATH&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&a

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

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.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

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