简体   繁体   中英

including files from different directories

So I set up my include files, such as header.php , nav.php , and footer.php in a /inc folder.

Then I include the files in my pages. But if I have another folder, for example wiki where i'd like to include those files onto the pages of wiki, the file paths for the includes break.

Example: I have included this in a page from the wiki directory.

<?php include_once('../inc/header.php'); ?>

I have two other includes in the header.php file, but since I'm including them in a different directory, both includes break, unless I go and append a ../ in the include path.

I'm wondering what is the best way to include files within different directories and not having to go back and fix the path?

Create constants using realpath in the initial file and then reference them using easy to read and manage but now absolute paths.

You can see an example here:

How can I get the "application root" of my URL from PHP?

I'd also suggest testing for the constant definition before defining.

The simplest solution? Use absolute paths.

If the files are in directories of the same level, the relative pathes (with ../) will be the same. If you have one within another, you will need to have the ../ on the beginning of the one in the sub-directory. The only way around changing the paths is to create a link as a sub-directory representing the main directory, although this can have dangerous consequences relating to recursive processes.

btw, include_once means that this operation is "optional", meaning the program will continue to execute. If you require the files included, use require_once, and the program will break if it's not there.

Include them like this

require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'inc'.DIRECTORY_SEPARATOR.'header.php');

This could work!

I like using this method:

define('ROOT_DIR', dirname(__FILE__) . '/'); // Defined on the index or in the bootstrap
include(ROOT_DIR . 'subfolder/fileName.php');

If you use PHP's native set_include_path to add an include folder you can then include files just by their name.

<?php
/**
 * The path to your /inc/ folder.
 */
$path = '/absolute/path/to/inc/';

/**
 * get_include_path retrieve's the current include path. 
 * It's a bad idea to over write the entire include path
 * The PATH_SEPARATOR constant is set per operating system
 */
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

/**
 * You can now include any files in /inc/ from any directory with no path
 */
include_once('header.php');
?>

$_SERVER['DOCUMENT_ROOT'] gives you the DocumentRoot of your webserver, eg Apache. Then you can take that as a stable datum and map from there as that won't vary.

include_once($_SERVER['DOCUMENT_ROOT'].'/inc/header.php');

我发现在IIS上运行时必须这样做str_replace(“ \\\\”,“ /”,$ _ SERVER ['DOCUMENT_ROOT']),如果有帮助的话。

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