php 包含另一个目录中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15683465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php include file from another directory
提问by coldpumpkin
Here is a structure example:
这是一个结构示例:
/main
/css
style.css
/include
article1.php
article2.php
header.php
index.php
In my header.php I have the following code for the css:
在我的 header.php 中,我有以下 css 代码:
<link rel="stylesheet" type="text/css" href="css/style.css" />
And, for example, in my index.php I have the following as the first line:
并且,例如,在我的 index.php 中,我将以下内容作为第一行:
<?php include 'header.php'; ?>
Now, everything works fine as it is. Next I'm going to insert the following code in the article1.php file:
现在,一切正常。接下来我将在article1.php文件中插入以下代码:
<?php include '../header.php'; ?>
The contents (menus and other html) are displayed correctly, however the CSS won't be displayed/recognized at all. Basically what's happening is that the header file is being included but the server isn't respecting the directory parenting. For the CSS to be displayed correctly I'd have to change the link rel
for the CSS to ../css/style.css
, but if I do so it won't work on files located in the main directory.
内容(菜单和其他 html)显示正确,但是 CSS 根本不会显示/识别。基本上发生的事情是头文件被包含在内,但服务器不尊重目录的父级。为了正确显示 CSS,我必须link rel
将 CSS 的更改为../css/style.css
,但如果我这样做,它将无法处理位于主目录中的文件。
I hope I made my problem clear. What am I doing wrong? How can I include files from different directories and preserve the links inside them?
我希望我把我的问题说清楚了。我究竟做错了什么?如何包含来自不同目录的文件并保留其中的链接?
回答by Matt
In your site's <head>
section, insert a <base>
elementwith the href
attribute. Set the href
attribute to the base URL of your website, and all relativerequests will be sent through that base URL.
在您站点的<head>
部分中,插入一个<base>
具有href
属性的元素。将该href
属性设置为您网站的基本 URL,所有相关请求都将通过该基本 URL 发送。
<base href="http://my-website-url.com/" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
With a correctly-set base
tag, the user's browser will attempt to load your stylesheet from the URL http://my-website-url.com/css/style.css
.
使用正确设置的base
标签,用户的浏览器将尝试从 URL 加载您的样式表http://my-website-url.com/css/style.css
。
Note: this not only affects stylesheets, but all relative links in the document.
注意:这不仅会影响样式表,还会影响文档中的所有相关链接。
回答by mikevoermans
It has to do with how pathing works in includes. I recommend pathing things from the doc root whenever possible.
它与路径在包含中的工作方式有关。我建议尽可能从文档根目录中进行路径处理。
<?php include( $_SERVER['DOCUMENT_ROOT'] . '/header.php' ); ?>
That will work in any of your files.
这将适用于您的任何文件。
回答by bubba
Instead of using relative paths:
而不是使用相对路径:
href="css/style.css"
use absolute paths from the webroot:
使用来自 webroot 的绝对路径:
href="/css/style.css"
回答by UnholyRanger
First off, the problem is that your link to your CSS files is wrong. The best thing to do is look at the output of the HTML (view source) from the page. So lets break it down (form index.php):
首先,问题是您指向 CSS 文件的链接是错误的。最好的办法是查看页面的 HTML 输出(查看源代码)。所以让我们分解它(表单 index.php):
Index.php
is located at domain.tld/index.php
. Your css files are located at domain.tld/css/*
. When viewing the index file, your header is
Index.php
位于domain.tld/index.php
。您的 css 文件位于domain.tld/css/*
. 查看索引文件时,您的标题是
<link rel="stylesheet" type="text/css" href="css/style.css" />
which works because you are at the top of the directory (or root) and the links are made relative to this directory. Now when you go up to domain.tld/include/article1.php
, you are no longer at the root. It is trying to access:
之所以有效,是因为您位于目录(或根目录)的顶部,并且链接是相对于该目录建立的。现在,当您上升到 时domain.tld/include/article1.php
,您不再是根。它正在尝试访问:
domain.tld/include/css/style.css
This means you have to build the full link or change your relative. The easy way since the CSS is at the root is just to use the following
这意味着您必须建立完整的链接或更改您的亲戚。由于 CSS 位于根目录,因此最简单的方法就是使用以下内容
<link rel="stylesheet" type="text/css" href="/css/style.css" />
^
This means it will look at the root of the domain for the css directory as such domain.tld/css/styles.css
. If your top directory is /main
, use /main/css/styles.css
这意味着它将查看 css 目录的域的根目录domain.tld/css/styles.css
。如果您的顶级目录是/main
,请使用/main/css/styles.css
回答by wallerjake
You should include your css file from the root. So /css/style.css so way it will always start at the root and then go down from there. I believe that should fix your problem in all cases.
您应该从根目录包含您的 css 文件。所以 /css/style.css 这样它总是从根开始,然后从那里开始。我相信在所有情况下都应该解决您的问题。
回答by Xavier S.
May I explain your problem and how to solve it.
我可以解释一下你的问题以及如何解决它。
An include in php works as in C. When you include a page that copy/paste content of the page into the first one.
php 中的 include 与 C 中的一样。当您包含一个页面时,该页面将页面的内容复制/粘贴到第一个页面中。
So, there are two files:
所以,有两个文件:
include.php
包含.php
<?php
$var = 'PHP';
?>
main.php
主文件
<?php
include 'include.php';
echo $var; // affiche 'PHP'
?>
When you request for "main.php" page you will get the following:
当您请求“main.php”页面时,您将获得以下信息:
<?php
$var = 'PHP';
echo $var; // affiche 'PHP'
?>
So if you need to include an element to your page. You have two choices:
因此,如果您需要在页面中包含一个元素。你有两个选择:
1. absolute path (better way)
1.绝对路径(更好的方式)
Instead of using relative path, use absolute path. This way allows you to include a file from everywhere without of use of the current path.
使用绝对路径而不是使用相对路径。这种方式允许您在不使用当前路径的情况下包含来自任何地方的文件。
2. variable
2. 变量
You can use a variable containing the relative path to the root directory of your repository/website. So each time you have to include a page or create one you have to define a variable as following:
您可以使用包含存储库/网站根目录的相对路径的变量。因此,每次您必须包含一个页面或创建一个页面时,您都必须定义一个变量,如下所示:
include.php
包含.php
<?php
$var = 'PHP';
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"".$currentPath."css/style.css\" />";
?>
main.php
主文件
<?php
# path to the root of the website
$currentPath = "../";
include 'include.php';
echo $var; // affiche 'PHP'
?>
To learn more about include, see this page
要了解有关包含的更多信息,请参阅此页面
回答by Philipp
You have two options
你有两个选择
- Pass the relative path to the included file
- 传递包含文件的相对路径
ie.
IE。
<link rel="stylesheet" type="text/css" href="<?=$path ?>css/style.css" />
and
和
$path = "../";
include "header.php";
or 2. you use absolute paths to your files
或 2. 您使用文件的绝对路径
<link rel="stylesheet" type="text/css" href="/css/style.css" />
回答by SteeveDroz
Using relative includes from filed already called as includes is always a problem, each time you specify a relative location, replace your
使用已经称为包含的文件中的相对包含总是一个问题,每次指定相对位置时,请替换您的
../some_directory/some_file.php
with
和
dirname(dirname(__FILE__)) . '/some_directory/some_file.php'
dirname(__FILE__)
is the current directory.
dirname(__FILE__)
是当前目录。