在 php 中使用包含
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以考虑设置全局 包含路径使用包含。
You may consider setting a global include path while using include.
对于 php 5.3,您可以执行以下操作:
其中
__DIR__
是当前文件的目录对于旧版本,您可以使用
where
__FILE__
是当前文件的路径假设您有以下结构:
如果您的
header.php
包含具有如下相对路径的santitize_script.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:
where
__DIR__
is the directory for the current fileFor older versions you can use
where
__FILE__
is the path for the current fileLets say you have the following structure:
If your
header.php
includessantitize_script.php
with a relative path like so: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 whereheader.php
will be used thereafter使用绝对路径...
Use absolute path...
正如 yes123 所说,使用绝对路径。
Use absolute path as yes123 said.
您必须在这里使用绝对路径,而不是相对路径。我经常使用旧的服务器变量设置一些常量来表示重要的目录。像这样:
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');