在 php 中使用包含

发布于 2024-12-13 17:33:52 字数 690 浏览 3 评论 0原文

我有一个名为 header (我的网站的标题)的文件。我在我的所有网站中使用该文件。问题是它有这样的include:

include_once("../untitled/sanitize_string.php");

这意味着可能会抛出错误,具体取决于谁调用头文件。

我有目录、子目录和子目录的子目录..有没有一种简单的方法可以防止这种情况发生..而不是采用 satize 字符串包含并将其放在每个页面上而不是放在头文件中

Warning: require_once(/untitled/sanitize_string.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\headerSite.php on line 7

致命错误:require_once() [ function.require]: 无法打开所需的 '/untitled/sanitize_string.php' (include_path='.;C:\xampp\php\PEAR')第 7 行 C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\headerSite.php

I have a file that is called header (the header of my site).. I use that file in all my site. The problem is that it has this include in it:

include_once("../untitled/sanitize_string.php");

which means that a mistake may be thrown, depending on who calls the header file.

I have directories, subdirectories and subdirectories to the subdirectories.. is there a simple way to prevent this from happening.. Instead of taking the satize string include and placing it on every page and not in the header file

Warning: require_once(/untitled/sanitize_string.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\headerSite.php on line 7

Fatal error: require_once() [function.require]: Failed opening required '/untitled/sanitize_string.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\headerSite.php on line 7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

ㄟ。诗瑗 2024-12-20 17:33:52

您可以考虑设置全局 包含路径使用包含

You may consider setting a global include path while using include.

忆伤 2024-12-20 17:33:52

对于 php 5.3,您可以执行以下操作:

include_once(__DIR__ . '/../untitled/sanitize_string.php');

其中 __DIR__ 是当前文件的目录

对于旧版本,您可以使用

include_once(dirname(__FILE__) . '/../untitled/sanitize_string.php');

where __FILE__ 是当前文件的路径

假设您有以下结构:

/app/path/public/header.php
/app/path/public/user/profile.php
/app/path/untitled/sanitize_string.php

如果您的 header.php 包含具有如下相对路径的 santitize_script.php

include_once("../untitled/sanitize_string.php");

php 解释器将尝试将该文件相对地包含到当前工作目录因此,如果您执行像 http://localhost/header.php 这样的请求,它将尝试包含 /app/path/public/../untitled/sanitize_string.php< /code> 就可以了。

但是,如果您尝试执行类似 http://localhost/user/profile.php 的请求,并且 profile.php 包含 header.php,则 header.php 将尝试包含 中的文件>/app/path/public/user/../untitled/sanitize_string.php 这将不再起作用。 (/app/path/public/user 现在是当前工作目录)

这就是为什么在包含文件时使用绝对路径是一个好习惯。在 header.php 中使用时,__DIR____FILE__ 常量将始终具有相同的值:/app/path/public< /code> 和 /app/path/public/header.php 分别,无论此后在何处使用 header.php

For php 5.3 you can do:

include_once(__DIR__ . '/../untitled/sanitize_string.php');

where __DIR__ is the directory for the current file

For older versions you can use

include_once(dirname(__FILE__) . '/../untitled/sanitize_string.php');

where __FILE__ is the path for the current file

Lets say you have the following structure:

/app/path/public/header.php
/app/path/public/user/profile.php
/app/path/untitled/sanitize_string.php

If your header.php includes santitize_script.php with a relative path like so:

include_once("../untitled/sanitize_string.php");

the php interpreter will try to include that file RELATIVELY to the current working dir so if you will do a request like http://localhost/header.php it will try to include it from /app/path/public/../untitled/sanitize_string.php and it will work.

But if you will try to do a request like http://localhost/user/profile.php and profile.php includes header.php, than header.php will try to include the file from /app/path/public/user/../untitled/sanitize_string.php and this will not work anymore. (/app/path/public/user beeing the current working dir now)

That's why is a good practice to use absolute paths when including files. When used in header.php, the __DIR__ and __FILE__ constants will always have the same values: /app/path/public and /app/path/public/header.php respectively no matter where header.php will be used thereafter

眼眸里的快感 2024-12-20 17:33:52

使用绝对路径...

include_once('/home/you/www/include.php');

Use absolute path...

include_once('/home/you/www/include.php');
丢了幸福的猪 2024-12-20 17:33:52

正如 yes123 所说,使用绝对路径。

include_once(dirname(__FILE__)."/../untitled/sanitize_string.php");

Use absolute path as yes123 said.

include_once(dirname(__FILE__)."/../untitled/sanitize_string.php");
瘫痪情歌 2024-12-20 17:33:52

您必须在这里使用绝对路径,而不是相对路径。我经常使用旧的服务器变量设置一些常量来表示重要的目录。像这样:

define('MY_DIR',$_SERVER['DOCUMENT_ROOT'].'/path/to/yer/dir');

然后,修改您的 include 语句:

include_once(MY_DIR. '/your_file.php');

You're going to have to use absolute paths here, as opposed to relative. I often set up some constants to represent important directories, using the old Server vars. Like so:

define('MY_DIR',$_SERVER['DOCUMENT_ROOT'].'/path/to/yer/dir');

Then, modify your include statement:

include_once(MY_DIR.'/your_file.php');

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