相对路径:向下和向上 [英] Relative paths : going down and up
问题描述
我有一堆目录,但我遇到包含一些文件的问题。这是我的结构。
I have a bunch of directories, and I have trouble including some files. Here's my structure.
index.php
index.php
vian / vian.php
vian/vian.php
包含/ overall / head_content.php
includes/overall/head_content.php
包含/ overall / head.php
includes/overall/head.php
这是我的 head_content.php
。
testing text
<?php include 'includes/overall/head.php'; ?>
<div class="wrapper row3">
<div id="container" class="clear">
<div id="content">
我想将该文件包含在 vian / vian.php $中c $ c>
我使用'../ includes / overall / head_content.php';
,以及路径有效,因为测试文本出现了。但它不能包含 includes / overall / head.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';
用于我的所有页面,包括 index.php
,位于我的根目录中。
'../includes/overall/head_content.php';
is used on all my pages, including index.php
, which is in my root directory.
推荐答案
在你的结构中:
+ 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.
PHP的帮助就是 __ DIR __
魔力常数。它自动包含每个文件中的目录。您可以使用它,使相对路径成为绝对路径。绝对路径非常强大。
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.
示例:
- 来自 index .php 到包含/ overall / head.php :
__ DIR__。 '/includes/overall/head.php'
- 从包含/ overall / head.php 到 vian / vian .php :
__ DIR__。 '/../../ vian / vian.php'
- 从包含/ overall / head_content.php 到 includes / overall / head.php :
__ DIR__。 '/head.php'
- 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'
依此类推。
这篇关于相对路径:向下和向上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!