drjmrg8766 2013-10-03 14:55
浏览 69
已采纳

需要/包含php文件时更改文件夹路径

I am currently using an index.php to include another file , it structure like:

root / index.php
     / new/ index.php
          / img/ test.jpg

And I write following in root/index.php :

<?php
require_once("new/index.php");
?>

the problem is , all paths in it are wrong. As all paths are based on new/index.php.

For example, In new/index.php my test.jpg is like

<img src="img/test.jpg" />

It shows http://www.test.com/new/img/test.jpg when I directly access new/index.php

But it shows http://www.test.com/img/test.jpg when I include the php in index.php.

How to fix it? I tried chdir() but it does not work expect Thanks

  • 写回答

3条回答 默认 最新

  • dongsong8932 2013-10-03 14:57
    关注

    Make sure you always include with an absolute path, like:

    require_once(dirname(__FILE__) . "/otherfile.php");
    require_once(dirname(__FILE__) . "/../uponefolder.php");
    require_once(dirname(__FILE__) . "/sub/folder/file.php");
    

    Or use autoloading.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料