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__

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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题