好的,这是我的项目结构(简化):
+blog
-index.php //Index 2
+ views
-index.php // Index 3
-article.php
-index.php // Index 1
+partials
-constants.php
-top.php
好的,所以在索引 1 和索引 2 中,我使用这行代码来包含一些部分/片段:
索引 1:
<?php require_once("./partials/constants.php"); ?>
<?php require_once("./partials/top.php"); ?>
索引 2:
<?php require_once("../partials/constants.php"); ?>
<?php require_once("../partials/top.php"); ?>
但是在索引 3 中,如果我使用它:
<?php require_once(".../partials/constants.php"); ?>
<?php require_once(".../partials/top.php"); ?>
我收到此错误:
<body><br>
<b>Warning</b>: require_once(.../partials/constants.php): failed to open stream: No such file or directory in <b>/home/codio/workspace/blog/views/index.php</b> on line <b>1</b><br>
<br>
<b>Fatal error</b>: require_once(): Failed opening required '.../partials/constants.php' (include_path='.:/home/codio/.parts/packages/php5-apache2/5.5.15/lib/php') in <b>/home/codio/workspace/blog/views/index.php</b> on line <b>1</b><br>
</body>
文件路径前面的那些点并不表示目录的级别。
./
引用同一目录
../
比上一级。
也就是说,您应该在 index3 上运行../../partials/constants.php
对于 index2,由于它是 index3 以上的级别,因此需要将 require 语句更改为 ../partials/constants.php
对于索引,因为它位于同一级别的目录中,partials
您将运行require ('partials/constants.php');