I got a problem about including a included file in PHP.
我在PHP中包含一个包含文件时遇到了问题。
Project
项目
- functions(folder) has a.php
- functions(folder)有一个.php
- xml(folder) has b.xml
- xml(文件夹)有b.xml
- index.php
- 的index.php
This is my project structure(sorry about that, I can't post images). I try to use "index.php" to include "a.php" while "a.php" is using "b.xml"
这是我的项目结构(对不起,我无法发布图片)。我尝试使用“index.php”包含“a.php”,而“a.php”使用“b.xml”
this is what i did on XAMPP and it works perfectly:
这就是我在XAMPP上所做的,它完美地运作:
in index.php I wrote: include 'functions/a.php';
在index.php中我写道:include'function / a.php';
in a.php I wrote: $xml->load('xml/b.xml');
在a.php中我写道:$ xml-> load('xml / b.xml');
However if I copy these to my Uni's apache server, it can't open this b.xml. This is not permission because when i change to absolute path it works...
但是,如果我将这些复制到我的Uni的apache服务器,则无法打开此b.xml。这不是许可,因为当我改为绝对路径时,它有效......
Thank you guys in advance:-)
提前谢谢你们:-)
3 个解决方案
#1
1
in a.php you should refer to ../xml/b.xml if you use include thing is, it depeneds on when $xml->load() is defined. if it's your own code then put the path relative to the definition. otherwise "../xml/b.xml" should work.
在a.php中你应该引用../xml/b.xml如果你使用include thing is,它取决于定义$ xml-> load()的时候。如果它是您自己的代码,则将路径相对于定义。否则“../xml/b.xml”应该有效。
you can always to $_SERVER['DOCUMENT_ROOT'], but i myself like defining directories as constants (with absolute path) and using them around the project.
你可以随时使用$ _SERVER ['DOCUMENT_ROOT'],但我自己喜欢将目录定义为常量(使用绝对路径)并在项目周围使用它们。
define('DIR_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/');
define('DIR_FUNCTIONS', DIR_ROOT . 'functions/');
define('DIR_XML', DIR_ROOT . 'xml/');
#2
0
Try using set_include_path()
to set the include path to your application's root directory; then you should be able to include files relative to this path.
尝试使用set_include_path()设置应用程序根目录的包含路径;那么你应该能够包含相对于这个路径的文件。
#3
0
It's always better to use absolute paths, even if you have to construct it (e.g. $XML_PATH = $PATH_TO_BASE . 'xml/b.xml';
)
使用绝对路径总是更好,即使你必须构造它(例如$ XML_PATH = $ PATH_TO_BASE。'xml / b.xml';)
If you can't do that, you should add xml's parent to your path.
如果您不能这样做,则应将xml的父级添加到路径中。
#1
1
in a.php you should refer to ../xml/b.xml if you use include thing is, it depeneds on when $xml->load() is defined. if it's your own code then put the path relative to the definition. otherwise "../xml/b.xml" should work.
在a.php中你应该引用../xml/b.xml如果你使用include thing is,它取决于定义$ xml-> load()的时候。如果它是您自己的代码,则将路径相对于定义。否则“../xml/b.xml”应该有效。
you can always to $_SERVER['DOCUMENT_ROOT'], but i myself like defining directories as constants (with absolute path) and using them around the project.
你可以随时使用$ _SERVER ['DOCUMENT_ROOT'],但我自己喜欢将目录定义为常量(使用绝对路径)并在项目周围使用它们。
define('DIR_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/');
define('DIR_FUNCTIONS', DIR_ROOT . 'functions/');
define('DIR_XML', DIR_ROOT . 'xml/');
#2
0
Try using set_include_path()
to set the include path to your application's root directory; then you should be able to include files relative to this path.
尝试使用set_include_path()设置应用程序根目录的包含路径;那么你应该能够包含相对于这个路径的文件。
#3
0
It's always better to use absolute paths, even if you have to construct it (e.g. $XML_PATH = $PATH_TO_BASE . 'xml/b.xml';
)
使用绝对路径总是更好,即使你必须构造它(例如$ XML_PATH = $ PATH_TO_BASE。'xml / b.xml';)
If you can't do that, you should add xml's parent to your path.
如果您不能这样做,则应将xml的父级添加到路径中。