php include_once 路径
我试图找到一种方法使 include_once();
路径从起始目录开始,然后找到路径,例如而不是 ../../../path/to /file
我会有/path/to/file
。如果我这样做 /path/to/file
它说没有这样的文件或目录,这是我的直接代码
<?php
include_once("/assets/page_assets.php");
?>
I am trying to find a way to make the include_once();
path start from the beginning directory and then find the path e.g. instead of ../../../path/to/file
I would have /path/to/file
. If I do do that /path/to/file
it says no such file or directory, here is my direct code
<?php
include_once("/assets/page_assets.php");
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您有 Apache 服务器,
“/assets/page_assets.php”
表示来自磁盘根目录
,而不是来自服务器的根文件夹。如果您正在谈论其他一些开始目录
,则将其物理路径(服务器文件系统中的路径,而不是Web路径)定义为单独的常量/变量,并将其添加到包含的路径之前如上图所示。If you have an Apache server
"/assets/page_assets.php"
meansfrom the root of the disk
, not from the root folder of the server. If you are talking about some otherbeginning directory
then define physical path (path in the file system of the server, not the web path) to it as a separate constant/variable and prepend it to the included path as shown above.您可以显式指定包含路径:
但正如 Cheery 在评论中提到的,您必须包含不带前导斜杠的文件:
You can specify include path explicitly:
But as Cheery mentioned in comment, you must include your file without leading slash:
为此,您必须使用根目录中的路径。在某些情况下,这可能类似于
/var/www/mysite.com/assets/page_assets.php
。找到该路径的一种方法是使用__FILE__
(前后各有 2 个下划线)。如果您从文件中回显该内容,它将显示完整的路径。您应该能够使用它来设置正确的完整路径。To do this you must use the path from the root. In some cases this might look like
/var/www/mysite.com/assets/page_assets.php
. One way to find that path is to use__FILE__
(That's 2 underscores both in front and behind). If you echo that from a file, it will show you the complete path. You should be able to use that to set the correct full path.]); 启动脚本
使用
chdir($_SERVER['DOCUMENT_ROOT ' 从网络根目录看。
Start your script with
chdir($_SERVER['DOCUMENT_ROOT']);
Now you can call
include("path/to/file.php");
and it will start looking from the webroot.