当“根正斜杠”出现时,PHP 会包含该内容。在包含的路径之前,例如:“/folder/file.php”
为了加载图像的 css 或 javascript 等文件,我学会了添加一些额外的安全性并在文件夹前面添加额外的 /
,确保这些文件是从从根开始的文件夹加载的向下路径:
例如 /images/photo.jpg
例如 /scripts/script.js 例如
/layout/css.css`
现在,当我在 PHP 中执行相同的技巧时,文件似乎没有加载,然后添加了正斜杠!
<? include ("/folder/file.php"); ?> // include this file
然而,当我删除路径前面的 /
时,一切正常,但 Dreamweaver 说它找不到该文件!当我添加 /
时,dreamweaver 可以找到远程资源/文件,但在运行站点时,文件不能很好地包含在内。
For loading files like css or javascript of images, I have learned to add some extra safety and add an extra /
in front of the folders, making sure that these files are loaded from a folder starting from the root path down wards:
e.g. /images/photo.jpg
e.g. /scripts/script.js
/layout/css.css`
e.g.
Now, when I do the same trick in PHP, the file doesnt seem to load then the forward slash is added!
<? include ("/folder/file.php"); ?> // include this file
When I however remove that /
in the front of the path, it all works, but then dreamweaver says it cannot find the file! When I add the /
then dreamweaver CAN find the remote assets/file but when running the site the files doesnt include well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PHP 的面向文件的函数(include、require、fopen、opendir 等)通常都期望处理文件系统路径。也就是说,本地服务器上的路径,无需通过 Web 服务器进行中介。
这并不是说你不能为函数提供完整的绝对 URL,但一般来说,如果你没有看起来像 url 的东西 (protocol://host/path?query#fragment),它会被解释为本地文件系统中的某些内容。
因此,当您将
/
放在包含路径前面时,它会被视为绝对本地路径,从本地文件系统的顶部开始。如果没有前导
/
,它将被视为相对路径,PHP 将迭代其包含路径,尝试根据include_path + file_to_include
查找文件。PHP's file-oriented functions (include, require, fopen, opendir, etc...) all generally expect to be dealing with a filesystem path. That is, a path on the local server, without being mediated through a web server.
That's not to say you can't provide a full absolute URL to the functions, but in general, if you don't have something that looks like a url (protocol://host/path?query#fragment), it'll be interepreted as something in the local file system.
So when you stick a
/
in front of your include path, it's viewed as an absolute local path, starting at the top of the local file system.Without a leading
/
, it's viewed as a relative path, and PHP will iterate through its include path to try and find the file based oninclude_path + file_to_include
.以
/
开头的路径通常被解释为绝对路径(相对于环境)。因此,Dreamweaver 可能会将 root(即/
)解释为项目的根路径,而 unix 服务器(以及在其上运行的 php)将该路径解释为目录系统中的根目录。而在 HTTP 上,它通常指当前域的文档根。最好的解决方案是简单地使用相对路径,即以
.
开头的路径,例如../../folder/file.php
。A path starting with
/
is usually interpreted as an absolute path (relative to the environment). So Dreamweaver might interpret the root (i.e./
), as the project's root path, while a unix server (and as such php that runs on it) interprets the path as the root in the directory system. And on HTTP, it usually refers to the document root of the current domain.The best solution would be to simply use relative paths, i.e. paths starting with
.
, like../../folder/file.php
.如果您想包含一个具有我们所认为的“本地 url 路径”的文件,常见的习惯用法是:(
这是完全有效的 PHP3 样式语法。但现在
"{$_SERVER['DOCUMENT_ROOT']} “
更常见。但没有区别。)If you want to include a file with what we think of the "local url path", the common idiom for that is:
(That's perfectly valid PHP3-style syntax. But nowadays
"{$_SERVER['DOCUMENT_ROOT']}"
is more commonly read. But makes no difference.)