Gabriela Dias
Gabriela Dias

Reputation: 349

how to easy include a php besides all path thing?

I really have a problem to include php files. I need to work with a lot of folders so it becames hard sometimes to include a php page.

ex. file in folder: inc/chat/scripts/index.php want to include a file from php/ajax/nd.php

include("../../../php/ajax/nd.php");

so, my question is, there is another method to do this instead of using ../ all the time? (i dont know what server will run my page in the future to put a static path).

use include or include_once is better (to avoid another call)?

Upvotes: 4

Views: 97

Answers (3)

Sanjay Kumar N S
Sanjay Kumar N S

Reputation: 4739

The most optimized and easiest way to do this is by using the functions chdir() and getcwd().

First set your project root path in chdir('path') and use the getcwd() relative so that you don't need to include the full path.

Refer this

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31749

You can define a constant for the base path -

define('BASE_PATH', '/');

And use it like -

include_once(BASE_PATH.'/rest_of_path');

Then there will be no need for ../ for each folder. Just access them from root path.

Upvotes: 0

DevDonkey
DevDonkey

Reputation: 4880

just grab the document root and use that.

$root = $_SERVER['DOCUMENT_ROOT'] ; 
include("$root/php/ajax/nd.php");

this will include the file from the document root of the site/application.

Upvotes: 1

Related Questions