0

I have the following file hierarchy: screenshot

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?

user3646717
  • 1,095
  • 2
  • 12
  • 21
  • already explained here https://stackoverflow.com/questions/579374/what-does-the-dot-slash-do-to-php-include-calls – Waqar Ul Aziz Sep 02 '17 at 00:13
  • If ./ means the current directory, why require_once './AutentificadorJWT.php'; doesn't work? – user3646717 Sep 02 '17 at 00:34
  • Main script file you are running is index.php and the line require_once(./AutentificadorJWT.php): is being called in another file which is in sub folder "class", for php current directory is "htdocs" as index.php is current running script file. – Waqar Ul Aziz Sep 02 '17 at 00:41
  • I see, so relative paths actually are not using usuario.php as the reference file, but the index.php instead. I think it shouldn't be that way because it makes the relative path idea, useless. Don't you think? The main benefit of relative paths should be relative to the file in which you are using it and not "relative" to a fixed directory or location. – user3646717 Sep 02 '17 at 01:24
  • It's relative to the current directory of the process. When a PHP script starts, the current directory is set to the location of the script, but it doesn't change every time it goes into and out of an include file. – Barmar Sep 02 '17 at 01:32
  • So if I later move my index.php to a different folder (for example APIREST_VERSIONES) all my include_once will fail even if they use relative paths. That seems to be contrary to the main reason one would be using relative paths or am I crazy? – user3646717 Sep 02 '17 at 02:22
  • When a file is included in php through relative path php search file relative to the folder in which current running script file is placed, if it is not found then it search the file in the folder where the included file exists (if lines to include are in included file). PHP also search file in path set in environment variable. To disable all these hierarchy of searching we use ./ to restrict the compiler to search only relative to current running script. – Waqar Ul Aziz Sep 02 '17 at 23:14

1 Answers1

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.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55