[英]How to INCLUDE php files in nested folders
I am new to php and facing initial problems :).我是 php 的新手并面临最初的问题:)。
My directory structure is as follows :我的目录结构如下:
site
index
homepagesections.php
assets
css
style.css
config
db.php
include
header.php
index.php
In the index.php , which is in the root folder I need to include the header.php file which includes config/db.php and also assets/css/style.css.在根文件夹中的 index.php 中,我需要包含 header.php 文件,其中包含 config/db.php 和 assets/css/style.css。
I have tried all the options ( including. the DIR option ) but failed.我已经尝试了所有选项(包括DIR选项)但失败了。 What is the best way to include these files and also the path for the style.css ?
包含这些文件以及 style.css 的路径的最佳方法是什么?
In index.php path to header is include/header.php:在 index.php 中,头文件的路径是 include/header.php:
include('include/header.php');
In header your path to db.php is ../config/db.php so you will have:在标题中,您的 db.php 路径是 ../config/db.php,因此您将拥有:
include('../config/db.php');
Note '../' - this is how you navigate 1 level up, from the directory your file is.注意 '../' - 这是您从文件所在的目录向上导航 1 级的方式。 In header adding css will be something like:
在标题中添加 css 将类似于:
<link rel="stylesheet" href="../assets/css/style.css">
In index.php:在 index.php 中:
include (__DIR__.'/include/header.php');
In header.php:在 header.php 中:
include (__DIR__.'/../config/db.php');
include (__DIR__.'/../assets/css/style.css');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.