php include_once 路径

发布于 2025-01-01 03:46:40 字数 273 浏览 0 评论 0原文

我试图找到一种方法使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情丝乱 2025-01-08 03:46:40

如果您有 Apache 服务器,

<?php
include_once($_SERVER['DOCUMENT_ROOT'] . "/assets/page_assets.php");
?>

“/assets/page_assets.php” 表示来自磁盘根目录,而不是来自服务器的根文件夹。如果您正在谈论其他一些开始目录,则将其物理路径(服务器文件系统中的路径,而不是Web路径)定义为单独的常量/变量,并将其添加到包含的路径之前如上图所示。

If you have an Apache server

<?php
include_once($_SERVER['DOCUMENT_ROOT'] . "/assets/page_assets.php");
?>

"/assets/page_assets.php" means from the root of the disk, not from the root folder of the server. If you are talking about some other beginning 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.

也只是曾经 2025-01-08 03:46:40

您可以显式指定包含路径:

<?php ini_set('include_path',ini_get('include_path').':../../../:');  ?>

但正如 Cheery 在评论中提到的,您必须包含不带前导斜杠的文件:

<?php
include_once("assets/page_assets.php");
?>

You can specify include path explicitly:

<?php ini_set('include_path',ini_get('include_path').':../../../:');  ?>

But as Cheery mentioned in comment, you must include your file without leading slash:

<?php
include_once("assets/page_assets.php");
?>
天赋异禀 2025-01-08 03:46:40

为此,您必须使用根目录中的路径。在某些情况下,这可能类似于 /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.

独闯女儿国 2025-01-08 03:46:40

]); 启动脚本

使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文