0

I have a PHP application I'm trying to include, but it's causing problems because I'm trying to include its files from a subdirectory. For instance, here's the directory structure (more or less) I'm trying to work with:

/site
    /scripts
        /local
            some_script.php
    lib_file_a.php
    lib_file_b.php
    lib_load.php

lib_load.php has an include command for files lib_file_a.php and lib_file_b.php (relative path).

What I want to accomplish is to include lib_load.php from some_script.php, but there's a problem with the way I'm including lib_load.php. At first I tried require('../../../lib_load.php');.

As you would expect I saw the error, "Failed opening required 'C:\xampp\htdocs\site\scripts\local\lib_file_a.php'". As you can see, PHP is trying to include a file that does not exist. I found a Stack Overflow post that told me about using an absolute path (link), but this did not help. I next tried this code:

require(realpath(dirname(__FILE__) . '../../../' . 'lib_load.php'));

However, this time, the same error comes back. What am I doing wrong?

EDIT: I realized that the issue was actually in the library loader, which turned out to be including files based on an absolute path, not a relative path. (I should have read more carefully.) I resolved the issue by changing the path used by the library. This question should be closed if a mod sees this request.

Community
  • 1
  • 1
fruchtose
  • 1,270
  • 2
  • 13
  • 16

2 Answers2

9

To include lib load

require $_SERVER['DOCUMENT_ROOT'] . '/scripts/lib_load.php';

inside of lib load

require dirname(__FILE__) . '/lib_file_a.php';

if I got your problem right

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
2

Try this:

require dirname(__FILE__) . '/../../../lib_load.php');

Or if you're using PHP 5.3 or higher use this instead (it looks nicer):

require __DIR__ . '/../../../lib_load.php');
Paul
  • 139,544
  • 27
  • 275
  • 264