php cross files include paths confusion

109 views Asked by At

Two files from different directory levels are simultaneously calling the same file:

// index.php
include('./include/same_file.php');

// Webpages/page1.php
include('../include/same_file.php');

// include/same_file.php
include('../template/header.php'); // failed to open on index.php
fopen('../files/text1.txt');       // failed to open on index.php
file('./files/AA.html');           // failed to open on /include/same_file.php

I have tried hours searching for solution and found the dirname(__FILE__) and $_SERVER[DOCUMENT_ROOT] solutions but none of them works because none of them are giving detailed teaching to a beginner like me.

1

There are 1 answers

0
Marinus On

try this..

require_once(__DIR__."/include/same_file.php");

or

include(__DIR__."/include/same_file.php");