php交叉文件包含路径混淆

时间:2013-12-11 06:10:47

标签: php include document-root dirname

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

// 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]解决方案,但没有一个能够正常运行,因为它们都没有向像我这样的初学者提供详细的教学。

1 个答案:

答案 0 :(得分:1)

试试这个..

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

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