简体   繁体   中英

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)?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM