Why ./ is not being recognized as current directory in my php file?

两盒软妹~` 提交于 2019-12-11 16:57:36

问题


I have the following file hierarchy:

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?


回答1:


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';

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.

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.



来源:https://stackoverflow.com/questions/46009282/why-is-not-being-recognized-as-current-directory-in-my-php-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!