0

I have this folder-structure:

-out
    -DBconnection.php
-root
    -application
        -contact.php

Also here is the content of contact.php:

function index(){
    <form action="http://example.com/localhost/application/contact/check">
        // some code here ...
    </form>
}

function check(){
    // I need to include DBconnection.php script here
    require_once( /* what path */ );
    ...
}

But this path doesn't work: ../../out/DBconnection.php.

Note: path above works into index() function as well. However index() is defined as default in my routing system and when I open this path

http://example.com/contact 
/* which is the same as
   http://example.com/contact/index
*/

Then index will be executed. So how can I include DBconnection.php into check() function?

stack
  • 10,280
  • 19
  • 65
  • 117

4 Answers4

1

What I do in cases like this is to add at the very top of the root file the following lines:

define( 'DS',       DIRECTORY_SEPARATOR );
define( 'ROOT',     dirname(dirname(dirname(__FILE__))) ); // Root is ../../

And when I include a file, I use those constants to get the path (which is always relative to the rood document):

require ROOT . DS . "out" . DS . "DBconnection.php";
Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
  • Just may you please tell me is there any alternative for this? `dirname(dirname(dirname(__FILE__)))`. Actually I cannot use `../../../` instead of that. – stack Apr 26 '16 at 22:40
  • Thanks! But I'm not sure what you mean. Can you elaborate please? – Alon Eitan Apr 26 '16 at 23:31
  • http://stackoverflow.com/questions/36877297/can-i-use-dot-and-backslash-instead-of-dirname – stack Apr 26 '16 at 23:32
  • Take a look at this answer http://stackoverflow.com/questions/7925679/how-to-use-chdir-to-change-to-current-directory – Alon Eitan Apr 27 '16 at 16:39
0

try to use: ../../out/DBconnection.php

mgrantnz
  • 375
  • 1
  • 4
  • 15
0

You must have the following structure:

DIR1
    out
        DBconnection.php
    root
        application
            contact.php

So, from within contact.php to be able to use DBconnection.php, you must go up 3 directories. The correct way to do it is: ../../out/DBconnection.php.

Dan Costinel
  • 1,716
  • 1
  • 15
  • 23
0

It really depends on what is in your include path, and where your application gets bootstrapped.

If you're using relative paths (which you are, with the ../), you need to understand that just because it looks relative to the file you're working on, it might not be. It's relative to the file that you 'entered' through.

To get around this, you should be able to use something like

include_once(dirname(__FILE__) . '/../../out/DBConnection.php');

However, what you really should be doing is putting your application path in your include path. That way, you'll be able to do:

include_once('out/DBConnection.php');

And you'll be able to do this anywhere in your application!

Ben
  • 611
  • 4
  • 10
  • Ah. I may have missed a `../` in there. However, I'd really look at changing your include path using `set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/your/enclosing/folder')` if you want to make this more maintainable going forward. – Ben Apr 26 '16 at 22:41