简体   繁体   English

PHP包括在所包含的路径之前的“根正斜杠”,如:“/ folder / file.php”

[英]PHP include when a “root forward slash” precedes the included path like: “/folder/file.php”

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: 为了加载像css或javascript图像这样的文件,我学会了添加一些额外的安全性并在文件夹前面添加一个额外的/ ,确保这些文件是从根路径向下开始从文件夹加载的:

eg /images/photo.jpg 例如/images/photo.jpg
eg /scripts/script.js eg /layout/css.css` 例如/scripts/script.js eg / layout / css.css`

Now, when I do the same trick in PHP, the file doesnt seem to load then the forward slash is added! 现在,当我在PHP中执行相同的技巧时,文件似乎无法加载,然后添加正斜杠!

<? 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! 然而,当我删除/在路径的前面时,一切正常,但是Dreamweaver说它无法找到文件! When I add the / then dreamweaver CAN find the remote assets/file but when running the site the files doesnt include well. 当我添加/然后Dreamweaver时,CAN可以找到远程资产/文件,但在运行该站点时,文件并不包括在内。

PHP's file-oriented functions (include, require, fopen, opendir, etc...) all generally expect to be dealing with a filesystem path. PHP的面向文件的函数(包括,require,fopen,opendir等等)通常都希望处理文件系统路径。 That is, a path on the local server, without being mediated through a web server. 也就是说,本地服务器上的路径,而不是通过Web服务器调解。

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. 这并不是说你不能提供一个完整的绝对URL到函数,但一般来说,如果你没有看起来像url(protocol:// host / path?query#fragment)的东西,它会被解释为本地文件系统中的某些东西。

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. 因此,当您在include路径前面/时,它被视为绝对本地路径,从本地文件系统的顶部开始。

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 on include_path + file_to_include . 没有前导/ ,它被视为相对路径,PHP将遍历其包含路径,尝试根据include_path + file_to_include查找文件。

A path starting with / is usually interpreted as an absolute path (relative to the environment). /开头的路径通常被解释为绝对路径(相对于环境)。 So Dreamweaver might interpret the root (ie / ), 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. 因此,Dreamweaver可能将 (即/ )解释为项目的根路径,而unix服务器(以及在其上运行的php)将路径解释为目录系统中的根。 And on HTTP, it usually refers to the document root of the current domain. 在HTTP上,它通常是指当前域的文档根目录。

The best solution would be to simply use relative paths, ie paths starting with . 最好的解决方案是简单地使用相对路径,即以路径开头的路径. , like ../../folder/file.php . ,像../../folder/file.php

If you want to include a file with what we think of the "local url path", the common idiom for that is: 如果你想包含一个我们认为是“本地URL路径”的文件,那么常见的习惯用法就是:

 include("$_SERVER[DOCUMENT_ROOT]/folder/file.php");

(That's perfectly valid PHP3-style syntax. But nowadays "{$_SERVER['DOCUMENT_ROOT']}" is more commonly read. But makes no difference.) (这是完全有效的PHP3风格语法。但现在"{$_SERVER['DOCUMENT_ROOT']}"更常见。但没有区别。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM