Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

php - include file from different directory

following is my files directory structure...

Config
   config.php (is calling some files too like includes/class.DB.php)

Now i have created another folder Admin and created new config.php (and calling root config file by require_once '../config/config.php';) its loading config file correctly but showing errors on includes/class.DB.php .

I hope, you have got my an idea of my problem, what is the way to achieve this, by USING DIR_NAME/ $SERVER['DOCUMENT_ROOT']

please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

use

dirname(__FILE__); // or, if you have +5.3, use __DIR__ instead

so if you have this structure

includes
    -class.php
admin
    -admin.php
config
   -config.php

So you can use

//admin.php
include(dirname(__FILE__)."/../config/config.php");

//config.php
include(dirname(__FILE__)."/../includes/class.php");

it always aims to the same directory!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...