0
votes

I have

public_html/dir/dir1/1.php and
public_html/dir/dir2/2.php

how to include 1.php into 2.php?

DETAILS: I have 2 file as public_html/projectplay/functions/functions.php And public_html/projectplay/includes/header.php

And also, public_html/projectplay/includes/header_pc.php public_html/projectplay/includes/header_mob.php

In header.php, I must include functions.php I tried bellow code

<?php 
require '../functions/functions.php';
if ( $detect->isMobile() ) {
include 'header_mob.php';

}
else {
include 'header_pc.php';
}

?>

ANd my public_html/projectplay/index.php is here

<?php require_once 'functions/functions.php'; ?>
<!DOCTYPE html>
<?php include 'includes/header.php';?>
Dummy Dummy Dymmy
<?php include 'includes/footer.php';?>

And public_html/projectplay/includes/footer.php is

But it output an error on index.php

Warning: require(../functions/functions.php): failed to open stream: No such file or directory in /home/epdroidc/public_html/projectplay/includes/header.php on line 2

Warning: require(../functions/functions.php): failed to open stream: No such file or directory in /home/epdroidc/public_html/projectplay/includes/header.php on line 2

Fatal error: require(): Failed opening required '../functions/functions.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/epdroidc/public_html/projectplay/includes/header.php on line 2

Where is my problem? I am new in php.

2
Just remove the require '../functions/functions.php'; in header.php because you're including it within index.php. - ʰᵈˑ
But what if i want to use header.php in public_html/files/index.php (or anything new file that have not <?php require_once 'functions/functions.php'; ?>) ? then how header.php load functions.php.? I got an idea! better remove <?php require_once 'functions/functions.php'; ?> from index.php - Sy Kat
No. Every file you include has access to all php variables and functions that you have previously declared or included. So require_once in the index.php is a better practice. - winkbrace

2 Answers

0
votes

Use absolute instead of relative paths. See this question for reference.

Note that it seems that in index.php you already require functions/functions.php. Your application logic occurs to be a bit chaotic.

0
votes

in php2:

require_once('../dir1/php1.php');

or

require_once($_SERVER['DOCUMENT_ROOT'].'/public_html/dir/dir1/php1.php');