0

When I'm running my site it keeps telling me

Warning: require_once(../../functions.inc.php): failed to open stream: No such file or directory in F:\xampp\htdocs\FinalYear\inc\functions\LoginOrRegister.inc.php on line 5

But the file is exactly 2 folders above the file I'm requiring. When I'm doing the following:

set_include_path(dirname(__FILE__)."/../../");
require_once('functions.inc.php');

It works (I don't get an error message for a non-existing file BUT In the functions.inc.php there is a new object created of the database.php.

$db = new database();

I can call this on every other site, just not on my LoginOrRegister.php. It always tells me, that this object doesn't exist, even, when I create it myself in the file. Any help?

4

3 回答 3

4

It looks to me like you are including a file from an included file. If that is the case, the relative path has to be relative to the script that is running, and not the script that is included inside the first script.

It is much easier to use absolute paths (use dirname(__FILE__) before the relative path).

require_once(dirname(__FILE__)."/../../functions.inc.php");
于 2013-03-19T11:20:01.343 回答
1

You are probably including a file out of a file that is itself included by another file. PHP will include relativ to the file that has been called originally.

So consider this stucture:

|- htdocs
    |- index.php
    |- inc
        |- test.php (A)
        |- inc
            |- test.php (B)

If you now call /index.php and the index.php file will include inc/test.php this will include file A.

If inside the test.php (A) we also call include inc/test.php it will include test.php (A) again because the path is relative to index.php. If instead you call /inc/test.php from the webbrowser while there is an include on inc/test.php in there it will include test.php (B) into the called test.php (A)

This is because PHP handles an include like the sourcecode of the included file would replace the include line, so basically you move the sourcecode to another location.

于 2013-03-19T11:22:08.967 回答
0

I always do this to avoid that kind of issues:

$root = $_SERVER['DOCUMENT_ROOT']; //your website's root folder, where the index.php is
include("$root/path");
于 2013-03-19T11:36:04.167 回答