首页 > 解决方案 > 如何在嵌套文件夹中包含 php 文件

问题描述

我是 php 新手并面临最初的问题:)。

我的目录结构如下:

site
   index
      homepagesections.php
   assets
       css
         style.css
   config
      db.php
   include
      header.php

index.php

在根文件夹中的 index.php 中,我需要包含 header.php 文件,其中包括 config/db.php 和 assets/css/style.css。

我已经尝试了所有选项(包括DIR选项)但失败了。包含这些文件以及 style.css 路径的最佳方法是什么?

标签: phpfiledirectoryinclude

解决方案


在 index.php 中:

include (__DIR__.'/include/header.php');

在 header.php 中:

include (__DIR__.'/../config/db.php');
include (__DIR__.'/../assets/css/style.css');

推荐阅读