簡體   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