如果我想包含另一个文件夹,如何获取链接路径


How can i get link path if i want include another folder

我想知道如何给出另一个文件夹的include路径。

例如,我有一个名为test文件夹的项目。测试文件夹中有一个文件夹,即assets文件夹和index.php文件。在资源文件夹中有两个文件夹,即include和xyz文件夹。在include文件夹中有两个文件,分别是header.php和footer.php。在header.php文件中有三个链接主页、产品和客户。xyz文件夹中有product.php和customer.php文件。

在index.php、product.php和customer.php中,我希望包括header.php和footer.php。

但我不能走正路。

<?php include('../../header.php');?>

<?php include('../../footer.php');?>

请帮助我,我如何给出正确的路径,如果我点击产品链接,就会加载header.php和footer.php。

试试这个:

<?php
include_once(dirname(__FILE__) . '/database.class.php');
?>

参考编号:http://php.net/manual/en/function.dirname.php

如果您的目录看起来像这样:

src
->includes
  ->header.php
  ->footer.php
web
->index.php

如果您想在index.php中包含页眉和页脚,请执行以下操作:

<?php
   //index.php
   include __DIR__.'/../src/includes/header.php';
   echo "foobar";
   include __DIR__.'/../src/includes/footer.php';
?>

http://php.net/manual/en/language.constants.predefined.php

基本上我已经使用了如下

<?php include_once('../include/header.php');?>
<?php include_once('../include/footer.php');?>
echo realpath('../../header.php');
echo realpath('../../footer.php');

我认为如果你能更换

 ../../header.php by ../include/header.php and
../../footer.php by ../include/footer.php.

那么你会得到准确的结果。