php交叉文件包含路径混淆


php cross files include paths confusion

来自不同目录级别的两个文件同时调用同一个文件:

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

我花了几个小时寻找解决方案,找到了dirname(__FILE__)$_SERVER[DOCUMENT_ROOT]解决方案,但都不起作用,因为它们都没有为像我这样的初学者提供详细的教学

试试这个。。

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

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