Home  >  Article  >  Backend Development  >  与require有关的路径有关问题

与require有关的路径有关问题

WBOY
WBOYOriginal
2016-06-13 11:09:36985browse

与require有关的路径问题
我把网页的header部分独立出来,让其他php文件去require这个文件。
header.php文件放在 /common 文件夹中,
其他文件放在 /main 文件夹中。

我在header.php 文件中设置网页的icon,图标的位置在 /common/imgs
下面这段代码中的路径应该怎么写,才能让其他网页require这个文件的时候能找到icon的位置?

<link rel="shortcut icon" href=<?= $_SERVER["DOCUMENT_ROOT"] .<br />"/common/imgs/icon.ico" ?> />

------解决方案--------------------
设你的 common 文件夹 是网站的一级子目录



href、src 属性都是 url 指向,与文件系统无关

------解决方案--------------------
header.php加上<br />define("common_dir",dirname(__FILE__));


<link rel="shortcut icon" href="<?php echo common_dir;?>/imgs/icon.ico" />

------解决方案--------------------

引用:
引用:PHP code?12header.php加上define("common_dir",dirname(__FILE__));

PHP code?1/imgs/icon.ico" />

那现在如果在/ma……


不是的
define("common_dir",dirname(__FILE__));

是相对于 使用该函数的文件所在路径 即header.php所在路径
而不是相对引用header.php文件所在路径

你在任何地方引用了 header.php
common_dir都是不会变的
------解决方案--------------------
href、src 属性都是 url 指向,与文件系统无关

一般用相对路径 src="/文件夹/较片地址" 这样就行了
如果你的网站是在二级或多级目录下则在 相对路径前再加上对应的二级目录
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn