Home >Backend Development >PHP Tutorial >Use caution regarding require_once usage and relative directories in PHP

Use caution regarding require_once usage and relative directories in PHP

黄舟
黄舟Original
2017-06-25 11:11:151983browse

wwwroot //The absolute path to the website root directory is: F:/wwwroot

-- folder_a // Folder A

  file_a_a.php
   file_a_b.php
   file_a_c.php

-- folder_b // Folder B

file_b_a.php
   file_b_b.php
   file_b_c.php

-- index.php

****************************************** ***********************

This directory hierarchy is already very clear:

wwwroot is the root directory, and below The index.php file and the two folders folder_a and folder_b

These two folders have three php files respectively

Let’s first look at the contents of the index.php file:

<?php
     require_once("folder_a/file_a_a.php");
     echo "文件folder_a_a.php被包含成功";
?>

Let’s take a look at the contents of the folder_a/folder_a_a.php file:

<?php
     require_once("../folder_b/file_b_a.php");
     $x = new X();
     $x.printInfo();
?>

Finally, let’s take a look at the contents of the folder_b/folder_b_a.php file:

<?php
     class X{
          function printInfo(){
               echo &#39;success;
          }
     }
?>

Yiju Tutorial Network >Php Tutorial>>FAQ >

Be careful about the usage of php require_once and relative directories

www.111cn.net Update: 2012-06-25 Editor: xiewen Source: Reprint

This article introduces the usage of require_once that everyone often encounters in PHP development. Friends in need can refer to it.

wwwroot //The absolute path to the website root directory is: F:/wwwroot

-- folder_a // Folder A

file_a_a.php

file_a_b. php

file_a_c.php

-- folder_b // Folder B

file_b_a.php

file_b_b.php

file_b_c.php

-- index.php

************************************ ****************************

This directory hierarchy is already very clear:

wwwroot is Under the root directory, there are the index.php file and two folders, folder_a and folder_b.

These two folders have 3 php files respectively.

Let’s first look at the contents of the index.php file:

The code is as follows Copy code

3d295085975a47a1f4fbc289fb1637fb

Let’s look at the contents of the folder_a/folder_a_a.php file:

The code is as follows Copy the code

b3866c13948f8cb21145c3ee581e3b87

Finally let’s take a look at the contents of the folder_b/folder_b_a.php file:

The code is as follows Copy the code

78d83f527e6a9f1295a4d948780ac043

ok If I run floder_a/file_a_a.php

directly now, it will output: success

If I run index.php

under wwwroot, an error will be reported because the included file cannot be found:file_b_a.php

But if I run all require_once( ), add dirname(FILE).'/'

Then whether you run file_a_a.php or index.php, the output will be normal

****************** ************************************************

Problem:

The first time I used a relative path, so an error occurred when I included it repeatedly.

The second time I used an absolute path, so there was no error. But I still A little confused:

I first analyzed the following reasons for errors when using relative paths:

I run index.php, it can find the folder_a directory, and it can also find file_a_a.php in that directory. , so it copies the contents of folder_a/file_a_a.php to the first line of index.php (the line containing the statement), and then continues to run (that is, runs the included content), so at this time it is equal to In index.php, run require_once('../folder_b/file_b_a.php') in file_a_a.php; It will find this path file (file_b_a.php) based on the current location of index.php. Of course, it cannot be found, so It went wrong.

But isn't it the same when I use absolute paths? But why doesn't it go wrong? Maybe everyone is a little confused about this sentence, let me explain in detail (according to the running order of the program To explain).

The program runs index.php first (note that I added dirname(FILE) at this time, so the current path is absolute),

index.php runs the first sentence first Code: require_once(dirname(FILE).'/'.'folder_a/file_a_a.php');

dirname(FILE) is f:/wwwroot/, so the path contained in this code is:

f:/wwwroot/folder_a/file_a_a.php

This path is correct, so there is no problem, right?

ok The first step is completed correctly

Then It copies the code in file_a_a.php to this place in index.php:

and then continues to run: This is to run all the code in file_a_a.php in index.php, then we Let’s see what code it runs?

<?php
     require_once(dirname(FILE).&#39;/&#39;."../folder_b/file_b_a.php");
     $x = new X();
     $x.printInfo();
?>

对就是这些,需要注意的是,这些代码已经被复制到了index.php,也就是说,现在index.php的内容实际上就变成了:

<?php
     require_once(dirname(FILE).&#39;/&#39;."../folder_b/file_b_a.php");
     $x = new X();
     $x.printInfo();
     echo "文件folder_a_a.php被包含成功";
?>

我们来看个注意事项

假设有如下三个文件, c.php a.php b.php 对应的存放目录为:localhost/ localhost/ localhost/demo

c.php
require_once("a.php");
require_once("demo/b.php");
B::demo();a.php
class A
{
}

b.php的内容比较有意思,因为它自己要继承 CLASS A 所以自己把a.php也引入进去了

require_once("../a.php");
class B extends A
{
    public static function demo()
    {
    echo "xx";
    }
}

执行localhost/c.php 系统报错,报错信息如下
Warning: require_once(../a.php) [function.require-once]: failed to open stream: No such file or directory in F:wwwdemob.php on line 2
Fatal error: require_once() [function.require]: Failed opening required '../a.php' (include_path='.;C:php5pear') in F:wwwdemob.php on line 2但是,惊奇的发现,如果去掉b.php里面的require_once语句,执行正常,那么一定是require_once语句定义多了吗?原因就是Class A重定义了两次?可是不会啊。如果我只在c.php里面加require_once(‘a.php’);这条语句,哪怕我写两遍也是没错的,那到底是咋回事呢?
原因就是,b.php定义的目录和c.php执行文件的目录层级不一致,导致在c.php里面require_once语句有两条。使其相当于

require_once("a.php");
require_once("../a.php");
class B extends A
{
    public static function demo()
    {
    echo "xx";
    }
}
B::demo();

原因找到了,因为在c.php里面,其相对目录 “..”就是 c.php的上一层了,导致文件找不到报错。
所以,我们的结论是,在 PHP 里面,使用require_once的时候,存在不同层级关系,且有相对目录的使用那么一定要谨慎,小心。


require_once很简单用但在使用时大家尽量使用绝对路径了。

The above is the detailed content of Use caution regarding require_once usage and relative directories in PHP. For more information, please follow other related articles on the PHP Chinese website!

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