PHP最高目录
很抱歉因为这么微不足道的事情打扰你。我无法正确地表达问题以在任何搜索栏上获得结果,我尝试过谷歌和这里,但没有得到相关答案。
我目前正在设置一个 include 语句,来自不同文件夹的 PHP 文件需要包含相同的文件,即“(Top directory)/public_html/Include/Head.php”。
我不知道如何告诉 PHP 在顶级目录中查找 public_html 。我原本以为这就是“..”的用途,但它的行为似乎很奇怪。有人可以解释一下吗?
这是我正在使用的:
<?php include("../public_html/Include/Head.php") ?>
另外,include 是一个文件夹,我将用户通常不需要查看的所有文件放在其中,主要是为了将它们移开,并使我的主文件夹不那么混乱。
Sorry to bother you over something so trivial. I can't word the question properly to get a result on any search bar, I've tried google, and here, but got no related answers.
I'm currently setting up an include statement, and PHP files from different folders need to include the same files, namely "(Top directory)/public_html/Include/Head.php".
I'm not sure how to tell PHP to look for public_html in the top directory. I originally thought that was what ".." was for, but it seems to behave wierdly. Can somone please explain?
Here's what I'm using:
<?php include("../public_html/Include/Head.php") ?>
Also, include is a folder where I put all the files that users generally don't need to view, mainly to get them out of the way, and make my main folder less messy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
定义一些全局常量:
define('TOP_DIR', '/www/')
,然后在所有包含中使用它:include(TOP_DIR.'public_html/Include/Head.php');
将
define()
放在一些易于到达的位置,并将其包含在您需要的任何页面上。如果您使用始终具有单个入口点(如 CodeIgniter)的框架,那么这些事情就会变得简单得多。然后,您可以在入口点文件中包含一个包含常量和设置的文件,并且您知道这些东西将始终可用。Define some global constant:
define('TOP_DIR', '/www/')
then use that in all your includes:include(TOP_DIR.'public_html/Include/Head.php');
Put that
define()
in some easy to reach location and include it on any page you need. This stuff becomes much simpler if you use a framework that always has a single entry point like CodeIgniter. Then you can just have a file of constants and settings you include in your entry point file and you know that those things will always be available.您可以在前面添加文档根目录,以便拥有一致的起点,并且不必担心特定情况下的遍历,例如,
或者,对于应用程序范围的解决方案,您可以简单地添加
Include 到您的包含路径:
http://php.net/manual/en/function.set-include-path.php
You could prepend with the doc root so that you have a consistent starting point and won't have to worry about traversing in your particular case, e.g.,
or, for an application-wide solution, you could simply add
Include
to your include path:http://php.net/manual/en/function.set-include-path.php
PHP的文件导入有点奇怪,如果你想相对于你想要的文件导入,请使用这个:
原因是,如果你有一个文件在
(顶级目录)/public_html/myfile.php
包含此文件,相对包含将相对于myfile.php
而不是包含的文件。PHP's file importing is a bit weird, if you want to import relative to the file you want, use this instead:
The reason is that if you have a file in
(Top directory)/public_html/myfile.php
that includes this file, the relative includes will be relative tomyfile.php
and not the included file.有几件事会影响您想要完成的任务。
首先,绝对路径和相对路径。每当您在路径中看到目录导航快捷方式时,您都在使用相对路径。
..
表示向上目录,或父目录。其次,可以应用 root 或 chrooting 的概念。根据您的系统,最顶层的目录
/
(或\
)可能是也可能不是您提供文件的位置。例如,您可以将特定网站的最顶层文件夹设置为文件系统中的特定文件夹(使用 Apache)。这被视为将网站“植根”到该文件夹。任何用户或浏览器都无法“查看”其父文件夹中的文件。然而,PHP 通常并不植根于与网站相同的位置。
如果您的 PHP 文件位于多层文件夹中,但您需要它们全部包含来自同一位置的文件,那么您可能需要使用绝对路径。
您的路径的具体细节完全取决于系统。
There are several things which affect what you're trying to accomplish.
First, absolute and relative paths. Any time you see directory navigation shortcuts in a path, you're working with a relative path.
..
means to go up a directory, or to the parent directory.Second, the concept of rooting or chrooting may apply. Depending on your system, the topmost directory
/
(or\
) may or may not be where you are serving files from. As an example, you can set the topmost folder of a particular web site to be a specific folder in your filesystem (using Apache). This is considered "rooting" the web site to that folder. No user or browser can "see" files from its parent folders.PHP, however, generally is not rooted to the same location as the web site.
If your PHP files are in multiple levels of folder, yet you need them to all include files from the same location, then you may want to use absolute paths.
The specifics of what your path should be are entirely system dependent.