got a strange problem that has so far left me scratching my head.
I have an index page with a common.php require file a few folders away. In the require file there are six scripts, with all but two of them being in the same folder as common.php.
So for the scripts in the same folder I simply use the file name eg-require(‘filename.php’), which works fine. The two remaining scripts are in an adjacent folder named config so I try using require(‘../config/file.php’) but it doesn’t work.
The strange thing is if I use the relative path from the index file it works?
How comes I can use paths that are relative to common.php if the files are in the same folder but not when in a different folder?
I’m sure there’s a logical explanation that escapes me right now so any help would be greatly appreciated.
thanks
J
As far as I understand, you are including files inside included files. As far as I think, the translation of “../” depends on the current working directory, not the directory in which the included file resides — but I am not sure. Anyway, you can workaround the problem inside the included files with something along these lines:
Assuming:
index.phpfile resides inC:/inetpub/wwwroot/project1inc01.phpfile resides inC:/inetpub/wwwroot/project1/include/cfg01.phpfile resides inC:/inetpub/wwwroot/project1/config/index.php contains:
To include cfg01.php inside inc01.php regardless of whether the container script is /index.php, /admin/index.php, /products/laptops/index.php:
First dirname returns the directory name in which inc01.php exists. Second one returns its parent directory. You can use the constant
DIRECTORY_SEPARATORinstead of worrying whether to use forward or back slashes. You can cascade dirname three times to get../../and so on.