繁体   English   中英

如何在嵌套文件夹中包含 php 文件

[英]How to INCLUDE php files in nested folders

我是 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 的路径的最佳方法是什么?

在 index.php 中,头文件的路径是 include/header.php:

include('include/header.php');

在标题中,您的 db.php 路径是 ../config/db.php,因此您将拥有:

include('../config/db.php');

注意 '../' - 这是您从文件所在的目录向上导航 1 级的方式。 在标题中添加 css 将类似于:

<link rel="stylesheet" href="../assets/css/style.css">

在 index.php 中:

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

在 header.php 中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM