php包含文件和目录

Lets say I have two files in directory new_folder: a.php and b.php

In the file of a.php there is a statement:

include_once 'b.php'

In addition, I have a file demo.php in a parent folder, in the demo file there is a statement:

include_once 'new_folder/a.php'

I.E. the folder structure is:

demo.php
new_folder
   - a.php
   - b.php

Why if I write in the a.php file:

  • include_once 'b.php' - correct path
  • include_once './b.php' - incorrect path
  • include_once 'new_folder/b.php' - incorrect path

Try This

require_once('../b.php'); 

my answer is based on php.net: Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.

that means that if I write in a.php: include_once 'b.php' ; and then include a.php in demo.php then first it looks for the file in the include_path, then it will look for the file in the directory of the calling script (a.php), and only then it will look for the file in the working directory (that is the directory of the file that we run and starts all the includes- demo.php)

edit: I tried running it and found out that first if there is b.php in the working directory then it will include it and not the b.php from the calling scrip's own directory.

the last check for the file is in the new_folder (the calling script's own directory)