dongtong2021 2014-11-21 00:51 采纳率: 0%
浏览 62
已采纳

使用PHP需要不同的目录级别

I have a project with the following directory structure (simplified):

+css
+js
+partials
  -header.php
  -footer.php
+blog
  -index.php
-index.php

I want to require_once the header.php and footer.php in both the blog index.php and the root index.php using the following the code:

<?php require_once("partials/header.php"); ?>

But of course in the blog it doesn't work because the header.php points to the css and js files relevant to the root index.php not the blog index.php.

Is there away to negate this or do I need two partials folders one at the root level and one at the blog level.

  • 写回答

1条回答 默认 最新

  • dongneng5383 2014-11-21 00:58
    关注

    create a file called constants.php and include this at the top of your scripts. define this constant as your base path to the root directory of your files:

    define("BASE_PATH", "http://www.yourURL.com/");
    

    then to make sure the correct path is used simply prepend this constant to your urls:

    require_once(BASE_PATH."/any_subdirectory/anyfilename.php");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?