【问题标题】:How can i get link path if i want include another folder如果我想包含另一个文件夹,如何获取链接路径
【发布时间】:2013-02-22 11:50:42
【问题描述】:

我想知道如何提供另一个文件夹的包含路径。

例如,我有一个名为 test 文件夹的项目。在 test 文件夹中有一个文件夹,即 assets 文件夹和 index.php 文件。在 assets 文件夹中有两个文件夹,分别是 include 和 xyz 文件夹。在 include 文件夹中有两个文件,分别是 header.php 和 footer.php。在 header.php 文件中有三个链接 home、product 和 customer。在 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


    【解决方案1】:

    试试这个:

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

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

    【讨论】:

      【解决方案2】:

      如果您的目录如下所示:

      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

      【讨论】:

        【解决方案3】:

        基本上我用过如下

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

        【讨论】:

          【解决方案4】:
          echo realpath('../../header.php');
          echo realpath('../../footer.php');
          

          【讨论】:

            【解决方案5】:

            我认为如果可以替换

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

            然后你会得到准确的结果。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多