Akshay
Akshay

Reputation: 2229

Including file from various folders

This has been giving me a headache. I've read multiple questions but none helped me.

So I need to include php files from different folders. Here's how my structure looks like. I'm using WAMP.

/C
    /wamp
      /www
       /project1
        /main
         /backend
          -class.common.php
          -inc.config.php
            /providers
             /google
              -google.functions.php

So as clear from the structure above, class.common.php is under folder backend (Which is under folder main).

Now, I'm trying to include class.common.php in google.functions.php using THIS code :-

include '../../class.common.php';

However, this doesn't seem to work. I've even tried working with the __DIR__ constant. No luck. What's the most robust way to achieve my result?

PHP Error Log.

    [15-Jun-2016 22:16:25 Europe/Paris] PHP Warning:  include(../../_inc_config.php): failed to open stream: No such file or directory in C:\wamp\www\yele\main\backend\api_providers\rech\api_functions.php on line 3

[15-Jun-2016 22:16:25 Europe/Paris] PHP Stack trace:

[15-Jun-2016 22:16:25 Europe/Paris] PHP   1. {main}() C:\wamp\www\yele\main\backend\ajax.php:0

[15-Jun-2016 22:16:25 Europe/Paris] PHP   2. include() C:\wamp\www\yele\main\backend\ajax.php:2

[15-Jun-2016 22:16:25 Europe/Paris] PHP   3. include() C:\wamp\www\yele\main\backend\class.apibase.php:3

[15-Jun-2016 22:16:25 Europe/Paris] PHP   4. include() C:\wamp\www\yele\main\backend\api_providers\rech\base_lib.php:2

[15-Jun-2016 22:16:25 Europe/Paris] PHP Warning:  include(): Failed opening '../../_inc_config.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\yele\main\backend\api_providers\rech\api_functions.php on line 3

[15-Jun-2016 22:16:25 Europe/Paris] PHP Stack trace:

[15-Jun-2016 22:16:25 Europe/Paris] PHP   1. {main}() C:\wamp\www\yele\main\backend\ajax.php:0

[15-Jun-2016 22:16:25 Europe/Paris] PHP   2. include() C:\wamp\www\yele\main\backend\ajax.php:2

[15-Jun-2016 22:16:25 Europe/Paris] PHP   3. include() C:\wamp\www\yele\main\backend\class.apibase.php:3

[15-Jun-2016 22:16:25 Europe/Paris] PHP   4. include() C:\wamp\www\yele\main\backend\api_providers\rech\base_lib.php:2

[15-Jun-2016 22:16:25 Europe/Paris] PHP Fatal error:  Cannot redeclare class common in C:\wamp\www\yele\main\backend\class.common.php on line 5

[15-Jun-2016 22:16:25 Europe/Paris] PHP Stack trace:

[15-Jun-2016 22:16:25 Europe/Paris] PHP   1. {main}() C:\wamp\www\yele\main\backend\ajax.php:0

[15-Jun-2016 22:16:25 Europe/Paris] PHP   2. include() C:\wamp\www\yele\main\backend\ajax.php:3

Upvotes: 0

Views: 42

Answers (2)

Jeff Puckett
Jeff Puckett

Reputation: 40841

I'm not sure I'm reading your diagram properly, but given the following:

if the full path to google.functions.php is

C:/wamp/www/project1/main/backend/providers/google/google.functions.php

and the full path to class.common.php is

C:/wamp/www/project1/main/backend/class.common.php

to include that file relative wherever it is, then __DIR__ should work

<?php
include_once __DIR__.'/../../class.common.php';

Update

From the looks of your error logs, you have a couple problems.

  • the include failure is happening in C:\wamp\www\yele\main\backend\api_providers\rech\api_functions.php which is calling a path relative include include(../../_inc_config.php), which changes depending on which file the script is included from. so this is odd for a vendor, but you can try changing the line to include(__DIR__.'/../../_inc_config.php') to force it to include relative to the actual including file, which is coincidentally my original answer for your problem.
  • you have a fatal error with redeclaring the common class, so use include_once or require_once

Upvotes: 1

mustafayelmer
mustafayelmer

Reputation: 159

firsty, getting real/physical path can be more usable

include realpath('/../../class.common.php');

Upvotes: 0

Related Questions