PHP绝对&相对路径 [英] PHP Absolute & Relative Paths

查看:109
本文介绍了PHP绝对&相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个目录结构:

  /style/style.php 
/ style / graphics / logo。 png
/cp-admin/index.php

style.php包含以下功能,与标志相呼应:

  function logo(){
echo< p>< img class =' logo'src ='style / graphics / logo.png'>< / p>;
}

我正在使用此功能,因此如果我更改徽标的路径,或者需要包含其他内容,所需要的只是修改该功能,并且更改会反映在所有页面中。



然而,当我包含from / cp-admin / index.php,上面的路径是相对于index.php而不是原始函数的路径。



我希望函数有一个路径是从函数本身引用的,而不是从包含它的所有页面引用。



预先感谢:)



/

code>这是根和之后使你的路径,因为这个地方(asbolute)



或使用你的相对路径,在这种情况下,你回去直到与'..'共同的文件夹



绝对尝试

 回声< p>< i mg class ='logo'src ='/style/graphics/logo.png'></p>; 

^从根目录

或相对值

  echo< p>< img class ='logo'src ='graphics / logo.png'>< / p>; 

^ style.php和文件夹图形位于同一父文件夹'style'中


I have this directory structure:

/style/style.php
/style/graphics/logo.png
/cp-admin/index.php

style.php contains the following function, which echoes the logo:

function logo(){
    echo "<p><img class = 'logo' src = 'style/graphics/logo.png'></p>";
}

I am using this function so that if I change the path of the logo, or need to include something else along with it, all it takes is to modify the function, and changes are reflected in all pages.

However, when I am including from /cp-admin/index.php, the path above is taken relatively to index.php, instead of the original function's path.

I want the function to have a path which is referenced-to from the function itself, rather than from all the pages where it's included.

Thanks in advance :)

解决方案

When you want to navigate in your folder, you can use

/ which is the root and after make your path since this place (asbolute)

or use your a relative path, in this case, you go back until the common folder with '..'

Try in absolute

echo "<p><img class = 'logo' src = '/style/graphics/logo.png'></p>";

                                    ^ from the root

or relative

echo "<p><img class = 'logo' src = 'graphics/logo.png'></p>";

                                    ^ style.php and the folder graphics are in the same parent folder 'style'

这篇关于PHP绝对&amp;相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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