【发布时间】:2015-02-24 07:25:17
【问题描述】:
我在理解相对路径概念方面遇到困难,我看到部分代码写成
../../abc/file/images/picutre/down.gif
如何计算相对路径
【问题讨论】:
-
请澄清您所说的“计算”是什么意思?路径是相对于它所在文件的路径。
我在理解相对路径概念方面遇到困难,我看到部分代码写成
../../abc/file/images/picutre/down.gif
如何计算相对路径
【问题讨论】:
相对路径是相对于工作目录的路径。换句话说,查找文件的起点是工作目录。
相对路径中的“../”表示上一级目录。
假设您从 index.html 页面中引用相对路径 ../../abc/file/images/picutre/down.gif,结构如下:
http://someexampleurl.com/dir1/dir2/index.html
您从 index.html 工作时的工作目录是 /dir2 因此考虑到您要上两层,浏览器希望文件位于:
http://someexampleurl.com/abc/file/images/picutre/down.gif
【讨论】:
如何计算相对路径
基本上,相对路径是从您所在的目录到您需要包含的文件的“映射”。因此,相对路径是根据您想去的地方计算的。
例如你有一个结构
/ (document root)
|--home.php
|--t.php
|--common
|--header.php
|--footer.php
|--support
|--index1.php
|--privacy
| |--index2.php
来自home.php,您需要包括header 和footer。因此,您的家庭代码看起来像
<?php
include("common/header.php"); // go one folder down (common) and grab the file header.php
include("common/footer.php"); // go one folder down (common) and grab the file footer.php
现在假设您在支持 index1.php,并且您需要 header.php 和 footer.php。你的代码看起来像
<?php
include("../common/header.php"); // go one folder up (common) and grab the file header.php
include("../common/footer.php"); // go one folder up (common) and grab the file footer.php
将文件夹内的文件夹视为级别(级别 1、级别 2 等)
注意:小心相对路径,因为它们很痛苦。
【讨论】:
它说从当前位置返回两级(父目录)“../../”。
【讨论】:
因此,如果我们在https://example.com/my/path/here 上并且它加载了一个文件../../abc/file/images/picutre/down.gif,那么我们将上升2 个目录,因为2 个../ 到https://example.com/my。然后我们会去/abc/file/images/picutre/down.gif。所以最终目的地是https://example.com/my/abc/file/images/picutre/down.gif
【讨论】:
【讨论】: