简体   繁体   English

php从主文件夹包含到其他文件夹中的文件

[英]php include from main folder to a file in different folder

in my root folder of my domain i have files header.php and footer.php, i have created a new folder and file in the new folder and wanted to include the header and footer files from main. 在我域的根文件夹中,我有文件header.php和footer.php,我在新文件夹中创建了一个新文件夹和文件,并希望包含来自main的页眉和页脚文件。 So the paths are: main folder: example.com/header.php new file: example.com/folder/new_file.php <- in this file i want to include the header. 因此路径为:主文件夹:example.com/header.php新文件:example.com/folder/new_file.php <-在此文件中,我想包含标题。

I have tried: 我努力了:

<?php include "header.php"; ?>
<?php include "/header.php"; ?>
<?php include "./header.php"; ?>
<?php include "../header.php"; ?>

and none of them work. 他们都不起作用。 Any help would be appreciated thanks. 任何帮助,将不胜感激谢谢。

<?php include( $_SERVER['DOCUMENT_ROOT'] . '/header.php' ); ?>

它会起作用的.try可能会在您的任何文件中提供帮助

include_once dirname(__DIR__).'/header.php';

这应该工作。

Thanks for all of your support. 感谢您的支持。 To answer my question thanks to all here are the answers. 要回答我的问题,谢谢大家,这里是答案。

<?php include( $_SERVER['DOCUMENT_ROOT'] . '/header.php' ); ?>

and php include "../header.php"; 并且php包含“ ../header.php”;

DO work and i was able to include a file from main directory so thanks to david and code360. 做工作,我能够包含主目录中的文件,因此感谢david和code360。

second part of why the css file was not being read is because i had it like this in header.php file: 为什么不读取css文件的第二部分是因为我在header.php文件中是这样的:

<link rel="stylesheet" href="css/bootstrap.css">

and missed the first "/" 并错过了第一个“ /”

Thanks again to code360 再次感谢code360

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

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