dongyuhui0418 2013-05-06 23:05
浏览 74
已采纳

在我的网站和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

  • 写回答

1条回答 默认 最新

  • dongwo2222 2013-05-06 23:11
    关注

    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__

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退