Search code examples
phpfilepathrequire-once

Assist with diagnosing PHP include, failed to open stream


I am attempting to move an old Intranet site running on Apache 2.2, to a WAMP setup (latest version) on my local machine.

One of the issues I have currently, is a require_once path failing to open, and I cannot determine what the cause is.

I have WAMP installed in:

C:\Wamp

I have changed http.conf and vhosts.conf to change the document root from

C:\Wamp\www

to

C:\Wamp\www\Intranet

This folder contains an index.php, which I can see being loaded correctly when browsing to localhost.

Index.php has an iFrame that loads welcome.php from

/site/welcome.php

This works, as the iFrame loads, but throws a 500 error.

Enabling PHP errors, the welcome.php page in the iFrame gives me an error on a require_once. The require_once is:

/site/login/config.php.

As you can see, I am using absolute paths here, so the fact that index.php is able to load /site/welcome.php, tells me it is loading the correct file from:

C:\Wamp\www\Intranet\site\welcome.php

I would expect then, my require_once with an absolute path to be loading:

C:\Wamp\www\Intranet\site\login\config.php

Which is a valid file path.

What is confusing me, is that the first absolute path I am using, seems to be starting from the document root, not the physical directory root.

The second absolute path I am using, does not seem to be starting from the document root.

Even more interestingly, if I change the require_once from:

/site/login/config.php

to

/login/config.php

It works?! I wouldn't expect it to, as that would suggest the absolute path I am specifying, is in fact a relative path?


Solution

  • I don't think this path (/site/login/config.php) makes sense on a windows machine.

    Better than hardcoding the full file path, determine it on runtime.

    E.g.:

    define('ROOT_DIR', realpath(dirname(__FILE__)));
    // because *nix and Windows path separators aren't the same (/ vs \)
    define('DS', DIRECTORY_SEPARATOR); 
    // just for convenience sake, you could use DIRECTORY_SEPARATOR on its own.
    

    Then including your config file, from welcome.php would be:

    require_once(ROOT_DIR . DS . 'login' . DS . 'config.php');
    

    I think that besides you are confusing the path that you use in the iframe declaration (which is a url path) with the path you use in the require (which is a filesystem path).