Hadi
Hadi

Reputation: 663

Confused with PHP Include

What the different between ./ vs ../ vs /

I'm very confused, i tried to access PHP class in different level folder.

I have the following folder structure in webdir/asia/v1:

+db
  class.database.php
  config.database.php
  +model
    Application.php
text.php

In class.database.php, I want include config.database.php which is in the same folder.

In Application.php, I want include class.database.php which is in the level above.

In text.php, I want include Application.php.

How do I include correctly?

What I know for now is that to include files in the same folder, I can use this:

include('config.database.php');

What about the other two variations? I tried in Application.php to include class.database.php like this :

include_once('../class.database.php');

but it doesn't work.

Upvotes: 0

Views: 100

Answers (4)

Lenart
Lenart

Reputation: 3111

You can try including the file like this:

include(dirname(__FILE__) . '/../class.database.php');

or even using document root from the server:

include($_SERVER['DOCUMENT_ROOT'] . 'db/model/Application.php');

Upvotes: 0

alex
alex

Reputation: 490153

No preceding path means current path, as does ./.

../ means up one level.

Upvotes: 2

Ilia Choly
Ilia Choly

Reputation: 18557

. is the current directory

./file.txt points to a file in the same directory

.. is the parent directory

../file.txt points to a file in the parent directory

/ is the root directory (like C:\ on windows)

Upvotes: 3

WordsWorth
WordsWorth

Reputation: 872

use include(dirname(dirname(__FILE__)) . "/class.database.php"); and see if it works.

Edit: Here,dirname(__FILE__) gives you the current directory where dirname of "dirname(__FILE__)" takes you one step up in hierarachy of directory tree.

Upvotes: 0

Related Questions