0

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..

4 Answers 4

1

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');
Sign up to request clarification or add additional context in comments.

Comments

0

Try this,

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

But both test1 and test2 on same level.

Comments

0

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();

Comments

0

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.