简体   繁体   中英

php included files and directories

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'

IE 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

尝试这个

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM