简体   繁体   English

PHP-包含在包含中不起作用

[英]PHP - Include within Include not working

I have the following directory structure: 我有以下目录结构:

home/admin/super/ 家庭/管理员/超级/

In home I have a file called config.php 家里,我有一个名为config.php的文件

<?php
//Get relative path - for PHP files
$documentRoot = $_SERVER['DOCUMENT_ROOT'].'/mwo2015/';
include($documentRoot.'library/constants.php');
echo $paypalID;
?>

and echo $paypalID is echoed; echo $paypalID ;

In path home/admin/ I have a file called access-control.php 在路径home / admin /中,我有一个名为access-control.php的文件access-control.php

<?php
//Common files
include('../config.php');
?>

and echo $paypalID is echoed again. 然后回显$paypalID

In path home/admin/super/ I have a file called output.php 在路径home / admin / super /中,我有一个名为output.php的文件

<?php
include('../access-control.php');
?>

Nothing is output and I get the following error in my error_log of path 什么都没有输出,并且我在path的error_log中得到以下错误

PHP Warning:  include() [<a href='function.include'>function.include</a>]: Failed opening '../config.php' for inclusion (include_path='.:/usr/lib64/php:/usr/share/pear')

Try this.. 尝试这个..

The "relative include path" is not shifted to the included file “相对包含路径”未移至包含文件

The solution is work with absolute paths and you can combine with relatives and absolute paths using dirname(__FILE__) , and you get the directory that contains the file. 该解决方案适用于绝对路径,您可以使用dirname(__FILE__)与亲戚路径和绝对路径结合使用,并获得包含文件的目录。

In within include file you can use following 在包含文件中,您可以使用以下内容

require_once dirname(__FILE__) . '/yourfile.php';
require_once dirname(__DIR__) . '/yourfile.php';

or 要么

include dirname(__FILE__) . "/yourfile.php";
include dirname(__DIR__) . "/yourfile.php";

The problem your having is this: 您遇到的问题是:

Relative paths use the entry point. 相对路径使用入口点。 So if you start and go to home/admin/super/output.php you start from there. 因此,如果您启动并转到home / admin / super / output.php,则从此处开始。

so home/admin/super. 所以家庭/管理员/超级。

include('../access-control.php'); 

goes to home/admin 回家/管理员

The include in this file goes to home/admin but it should go to home so to fix this you could do: 此文件中的include转到home / admin,但应该转到home,以便解决此问题,您可以执行以下操作:

../../config.php

This is the reason why you build your application around one entry point or make a global root and webroot to use throughout your application. 这就是为什么您围绕一个入口点构建应用程序或创建全局根目录和Webroot以便在整个应用程序中使用的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM