问题
I have a bunch of directories, and I have trouble including some files. Here's my structure.
index.php
vian/vian.php
includes/overall/head_content.php
includes/overall/head.php
Here is my head_content.php
.
testing text
<?php include 'includes/overall/head.php'; ?>
<div class="wrapper row3">
<div id="container" class="clear">
<div id="content">
I want to include that file in vian/vian.php
I use '../includes/overall/head_content.php';
, and the path works since 'testing text' shows up. But it cannot include includes/overall/head.php
. I vaguely understand why it doesn't work, but I can't figure out how to fix it.
'../includes/overall/head_content.php';
is used on all my pages, including index.php
, which is in my root directory.
回答1:
In your structure:
+ index.php
+ vian
| ` vian.php
` includes
+ overall
+ head_content.php
` head.php
Each file has a specific relationship to the other. Because of that you can make use of relative links from one file to another.
Of help in PHP for that is the __DIR__ magic constant. It contains the directory in each file automatically. You can use it, to make the relative path an absolute one. Absolute paths are very robust.
Examples:
- From index.php to includes/overall/head.php:
__DIR__ . '/includes/overall/head.php'
- From includes/overall/head.php to vian/vian.php:
__DIR__ . '/../../vian/vian.php'
- From includes/overall/head_content.php to includes/overall/head.php:
__DIR__ . '/head.php'
And so on and so forth.
回答2:
Try
<?php include 'head.php'; ?>
来源:https://stackoverflow.com/questions/13088553/relative-paths-going-down-and-up