Question

public_html > config.php 

public_html > A > test_file.php

in test_file.php code

require_once("../config.php");

works fine. shows dynamically-related files fine (config.php) in dreamweaver

public_html > includes > classes > allclasses.php

in allclasses.php code

require_once("../../config.php");

shows dynamically-related files fine (config.php) in dreamweaver

when I include allclasses.php in test_file.php

require_once("../config.php");
require_once("../includes/classes/allclasses.php");

shows dynamically-related files fine (config.php, allclasses.php) in dreamweaver

test_file.php stop working and on browser shows

Warning: require_once(../../config.php) [function.require-once]: 
failed to open stream: No such file or directory in .....\public_html\includes\classes\allclasses.php on line 11

using xampp as development server.

Was it helpful?

Solution

When doing includes inside includes it´s better to use absolute paths. So you can use the __DIR__ or __FILE__ constant to do it, just like this in test_file.php for example:

require_once(realpath(dirname(__FILE__) . '/../config.php'));

or just

require_once(realpath(__DIR__ . '/../config.php'));

OTHER TIPS

Use absolute url path instead of relative url path.

try this:

require_once($_SERVER["DOCUMENT_ROOT"]."/config.php");

Unless you call something like chdir(), all require/include calls are relative to the directory of the called PHP file (here test_file.php). That's why the require_once("../../config.php") from allclasses.php throws an error.

In your situation, the simplest fix would be to remove the require_once("../../config.php") from allclasses.php, if it has no other impacts.

I would recommend to use a front controller, to ensure that the called PHP script is always the same file (and include/require are always made for the same directory), and to do only once for all the include of the config.php file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top