avktech
avktech

Reputation: 92

how to include some php file to another folder using include

I am using include or include_once in php core project

include('test1/footer.php'); this is working in root folder ..

but we using above code in anyfolder like name test2 and include test1 file in test2 here above code not working..include('test1/footer.php');

please help me .

thanks..

Upvotes: 0

Views: 2824

Answers (4)

mike
mike

Reputation: 1

best practice for me is to define a constant containing server ROOT path (or other path that is base for your includes)

add code to root/index.php or to a configuration file if you have one

define('MYROOT', dirname(__FILE__));

use it anywhere in the project code, its also good when you change files hierarchy:

include(MYROOT.'test1/footer.php');

Upvotes: 0

Muhammad Akif
Muhammad Akif

Reputation: 86

http://prntscr.com/8ho3gk

check this screen shot to understand structure

if you want to add "configs.php" in "cargo.php" write this include('include/configs.php'); and if you want to add "cargo.php" in "configs.php" write this include('../cargo.php');

better use include_once(); not only include();

Upvotes: 0

oshell
oshell

Reputation: 9103

folder structure

/root
    /test1
        footer.php
    index.php

index.php

//works fine because test1 is on the same level as index.php
include('test1/footer.php');

folder structure

/root
    /test1
        footer.php
    /test2
        index.php

index.php

//does not work since in our current folder there is no folder "test1"
include('test1/footer.php');
//does work since we go back to root first
include('../test1/footer.php');

Upvotes: 1

Keval Rathi
Keval Rathi

Reputation: 986

Try this,

include('../test1/footer.php');

But both test1 and test2 on same level.

Upvotes: 0

Related Questions