需要/包含php文件时更改文件夹路径 [英] Change of folder path when require/ include an php file

查看:155
本文介绍了需要/包含php文件时更改文件夹路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用index.php来包含另一个文件,其结构如下:

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

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

我在root / index.php中写下以下内容:

And I write following in root/index.php :

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

问题是,其中的所有路径都是错误的。因为所有路径都基于new / index.php。

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

例如,在new / index.php中,我的test.jpg就像

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

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

显示 http://www.test.com/new/img /test.jpg 当我直接访问new / index.php

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

但它显示 http:// www。 test.com/img/test.jpg 当我在php.php中包含php时。

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

如何解决?我尝试过chdir()但它不能正常工作谢谢

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

推荐答案

确保始终包含绝对路径,例如:

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");

或使用自动加载。

这篇关于需要/包含php文件时更改文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆