为什么./在我的php文件中不被识别为当前目录? [英] Why ./ is not being recognized as current directory in my php file?
问题描述
我具有以下文件层次结构:
I have the following file hierarchy:
并且require_once某种程度上不起作用(警告:require_once(./AutentificadorJWT.php):无法打开流:第2行的C:\ xampp \ htdocs \ clases \ usuario.php中没有此类文件或目录) 据我所知./指向当前目录.现在,以下作品: require_once'AutentificadorJWT.php'; 为什么./不起作用?
and the require_once is somehow not working (Warning: require_once(./AutentificadorJWT.php): failed to open stream: No such file or directory in C:\xampp\htdocs\clases\usuario.php on line 2) As far as I know ./ points to the current directory. Now the following works: require_once 'AutentificadorJWT.php'; Why ./ is not working?
推荐答案
当您具有复杂的代码层次结构时,很难知道当前目录是哪个目录.如果要有恒定的参考点,则可以使用$_SERVER['DOCUMENT_ROOT']
定义计算机上代码的基础.所以...
It can be quite difficult to know what directory is the current directory when you have a complex hierarchy of code. If you want to have a constant point of reference, then you can use $_SERVER['DOCUMENT_ROOT']
which defines the base of your code on your computer. So...
require_once $_SERVER['DOCUMENT_ROOT'].'/base.php';
将在项目的根目录下为base.php工作.我遇到的一个问题是,单元测试并不总是设置很多$_SERVER
变量.
Will work for base.php in the root of your project. The one problem I've had with this is that unit testing doesn't always have a lot of the $_SERVER
variables set.
或者,您可以使用__DIR__
,它是当前文件的目录.因此,如果您将其更改为...
Alternatively, you can use __DIR__
which is the directory of the current file. So if in your case you changed it to ...
require_once __DIR__.'/AuthentifacdorJWT.php';
这将始终与您使用的文件的目录有关.
This will always be relative to the directory of the file your working with.
这篇关于为什么./在我的php文件中不被识别为当前目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!