[英]PHP Relative paths for require file
I've been going over those two topics: 我一直在研究这两个主题:
and couldn't make my script to work, none of presented methods are working or maybe I'm doing something wrong. 并且无法使我的脚本正常工作,所提供的方法均无效,或者我做错了什么。
Anyway this is where my problem occurred: 无论如何,这就是我的问题所在:
Root/ //this is root location for server
APP/ //this is root location for script
Root/APP/core/init.php //this is where I include classes and functions from
Root/APP/classes/some_class.php //this is where all classes are
Root/APP/functions/some_function.php //this is where all functions are
and so obviously I need to include init.php
everywhere so I did in every file like this: 所以很明显,我需要在所有地方都包含
init.php
,所以我在每个文件中都这样:
require_once 'core/init.php';
it was working until I have decided to create a location for admin files like this: 它一直有效,直到我决定为这样的管理文件创建位置:
Root/APP/Admin/some_admin_file.php
and when I included init this way: 当我以这种方式包含init时:
require_once '../core/init.php';
script failed to open functions, no such file in APP/Core/
folder so I used DIR method presented in topic above and than even weirder thing happened, error: 脚本无法打开功能,
APP/Core/
文件夹中没有此类文件,因此我使用了上面主题中介绍的DIR方法,甚至发生了奇怪的事情,错误:
no such file in APP/Core/classes/Admin/
APP / Core / classes / Admin /中没有此类文件
What is that? 那是什么? :D I'm lost with this, could someone help a bit ;)
:D我迷失了这个,有人可以帮忙吗;)
Include paths are relative to the current working directory, which can be inspected using getcwd()
; 包含路径是相对于当前工作目录的,可以使用
getcwd()
进行检查; this can be a source of many issues when your project becomes bigger. 当您的项目变大时,这可能是许多问题的根源。
To make include paths more stable, you should use the __DIR__
and __FILE__
magic constants; 为了使包含路径更稳定,您应该使用
__DIR__
和__FILE__
魔术常数。 for instance, in your particular case: 例如,在您的特定情况下:
require_once dirname(__DIR__) . '/core/init.php';
The dirname(__DIR__)
expression is effectively the parent directory of the script that's currently being run. dirname(__DIR__)
表达式实际上是当前正在运行的脚本的父目录。
Btw, __DIR__
could also be written as dirname(__FILE__)
. 顺便说一句,
__DIR__
也可以写为dirname(__FILE__)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.