简体   繁体   中英

php question include an included file

I got a problem about including a included file in PHP.

Project

  • functions(folder) has a.php
  • xml(folder) has b.xml
  • 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 "

this is what i did on XAMPP and it works perfectly:

in index.php I wrote: include 'functions/a.php';

in a.php I wrote: $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...

Thank you guys in advance:-)

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.

you can always to $_SERVER['DOCUMENT_ROOT'], but i myself like defining directories as constants (with absolute path) and using them around the project.

define('DIR_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/');
define('DIR_FUNCTIONS', DIR_ROOT . 'functions/');
define('DIR_XML', DIR_ROOT . 'xml/');

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.

It's always better to use absolute paths, even if you have to construct it (eg $XML_PATH = $PATH_TO_BASE . 'xml/b.xml'; )

If you can't do that, you should add xml's parent to your path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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