在我的网站和php-include函数中设置正确的相对路径

在我的网站和php-include函数中设置正确的相对路径

问题描述:

I have a basic question and since I am a noob with php it is difficult to dig through the thousands of questions you already answered...So I ask you for further help, sorry...

I just need to recall header.php into HTML pages. The website structure look like this:

-index.php
-header.php
-style.css
-articles/article1.php
-image/header.jpg

For index.php I used

<?php include 'header.php'; ?>

and everything works fine! The problem arises with pages like article1.php, i.e. for pages that are not in the root. I tried with

<?php include ('http://localhost/phpBB3/header.php'); ?>

because I am working locally rightnow, and 'localhost/phpBB3' is the root) but it does not work. Also I tried with

<?php include ('/header.php'); ?>

or with

<?php include ('../header.php'); ?>

barely understanding the real meaning of all these ./ (I know, I know, shame on me!)

Moreover, the header.php points to both style.css and header.jpg (the latter inside the image folder) and refers to ids and classes defined, of course, in the style.css. I think I may have some problem in their path as well.

How can I put things in order?

Any help is greatly appreciated! Mya

我有一个基本的问题,因为我是一个使用php的菜鸟,很难通过成千上万的问题挖掘你 已经回答了......所以我请求你们进一步的帮助,对不起...... p>

我只需要将 header.php code>召回到HTML页面中。 网站结构如下所示: p>

  -index.php 
-header.php 
-style.css 
-articles / article1.php 
-image /  header.jpg 
  code>  pre> 
 
 

index.php code>我用过 p>

 &lt;?  php包含'header.php';  ?&gt; 
  code>  pre> 
 
 

一切正常! article1.php code>等页面出现问题,即对于不在的页面 根。 我试过 p>

 &lt;?php include('http://localhost/phpBB3/header.php');  ?&gt; 
  code>  pre> 
 
 

因为我正在本地工作,而'localhost / phpBB3'是根) 但它不起作用。 我也试过

 &lt;?php include('/ header.php');  ?&gt; 
  code>  pre> 
 
 

或 p>

 &lt;?php include('../header.php'  );  ?&gt; 
  code>  pre> 
 
 

几乎不了解所有这些 ./ code>的真正含义(我知道,我知道,对我感到羞耻!)

此外, header.php code>指向 style.css code>和 header.jpg code>(后者) 在 image code>文件夹中)并且引用了当前在 style.css code>中定义的id和类。 我想我的路径也可能有问题。 p>

如何整理好东西? p>

非常感谢任何帮助! Mya p> div>

The reason http://localhost/phpBB3/header.php does not work is because PHP's include requires a local path. You can achieve what you want with dirname( __FILE__ ).

include dirname( __FILE__ ) . '/../header.php';

More on dirname()

More on __FILE__