Edgar Cooper - 11 months ago 76
PHP Question

# No such file or directory (path error)

Well, the structure of site is simple:

# site.com

1. '
config
' folder

• config.php

• cesar.php

2. '
login
' folder

• index.php

3. index.php

Config.php:

include_once '../config/cesar.php';


At
site.com/index.php
:

Warning: include_once(../config/cesar.php): failed to open stream: No such file or directory


At
site.com/login/index.php
everything is OK.

If I will remove one dot (./config/cesar.php), main index will become OK and login page will get the error.
How to make both codes work?

The .. in your path are used to go up a directory because of that the file won't exist where you are looking for it.
If you update it to be include_once 'config/cesar.php'; it should work since that will allow it to go down into the config directory rather than try to find a directory with the name of config 1 level above where index.php is located.
./ works since . is the notation for the current directory.
To answer your question, it wouldn't be possible to have the code work by using a relative path since both the files are in different locations on the server in relation to the one you want to include. If you want to have something that does then you will need to use an absolute path rather than the relative path. This would be something like /path/to/webdirectory/site.com/path/to/file/config.php (i.e. /home/charles/websites/site.com/config/config.php) in *nix and C:\path\to\webdirectory\site.com\path\to\file\config.php on windows.
In PHP you should be able to get the absolute path in a dynamic way by using the $_SERVER['DOCUMENT_ROOT'] variable. Ex: include_once$_SERVER['DOCUMENT_ROOT'] . '/config/cesar.php';