不知何故,我不能包含我想要的文件,既不使用require_once
,也不使用include
。我想要包含的文件与我想要包含它的文件在同一个目录中。我试了各种组合,但还是不行。
我试图使用require_once
警告:require_once(D:PROJEKATwampwwwEifdb_connect.php): failed to open stream: D:PROJEKATwampwwwEifcreate_user.php中没有这样的文件或目录
我第一次尝试使用
require_once '/db_connect.php'
然后我用
require_once 'db_connect.php' cause I realized that I have to use in windows
然后试着
require_once __DIR__ . 'db_connect.php'
最后
require_once $_SERVER['DOCUMENT_ROOT'] . '/Eif/db_connect.php';
我认为我使用的路径是好的,但是它总是以错误的方式使用它。
有什么问题吗?
谢谢
快速回答
你应该可以这样做:
require_once 'db_connect.php';
<标题> 此外信息 require
, require_once
, include
和include_once
都是一样的,只有一个例外:require在失败时会发出致命错误,而as include会发出警告。
另一件要记住的事情是,这些实际上是通过php ini include_path
指令搜索的,所以当你试图包含另一个文件时,php将按照第一个指令的顺序搜索这些目录。
默认情况下,该指令通常看起来像:.:/usr/share/php:/usr/share/pear
。这可以通过许多函数来改变,甚至是您想要知道的php.ini
:
- set_include_path—设置包含路径指令;
- get_include_path—显示当前包含路径;
- getcwd -显示当前工作目录;
- chdir更改当前工作目录;
注意如果使用set_include_path
,你不是在添加列表,而是覆盖它的值;所以知道正确的设置方法是:
set_include_path(get_include_path() . PATH_SEPERATOR . '/path/to/new/location');
标题>如果你想包含的文件在同一目录下,这应该可以工作:
require_once 'db_connect.php'
在你试图到达路径的过程中一定有一些错误。您可以使用以下php函数来调试它。
<?php
print_r(get_included_files()); // Write this above the file that you are trying to include
set_include_path('path_to_your_directory');
// This will set the directory for once in<br /> your page after setting the path use require_once with the file name straight away
?>