为什么 PHP“需要”没有找到文件?
我有以下文件结构:
temp
main
index.php
a.php
b.php
这是文件;
index.php
echo "index.php ---> " . __DIR__ . "<br />";
require('../a.php');
echo "OK<br />"
a.php
echo "a.php ---> " . __DIR__ . "<br />";
require('./b.php');
echo "a is here<br />"
b.php
echo "b is here<br />"
当调用 index.php
时,我收到以下错误:
index.php ---> D:\Programs\WampServer 2\www\temp\main
a.php ---> D:\Programs\WampServer 2\www\temp
Warning: require(./b.php) [function.require]: failed to open stream: No such file or directory in D:\Programs\WampServer 2\www\temp\a.php on line 5
Fatal error: require() [function.require]: Failed opening required './b.php' (include_path='.;C:\php5\pear') in D:\Programs\WampServer 2\www\temp\a.php on line 5
我注意到如果我更改
require('./b.php');
为
require('b.php');
为什么是这样? 它按预期工作。
I have the following files structure:
temp
main
index.php
a.php
b.php
Here are the files;
index.php
echo "index.php ---> " . __DIR__ . "<br />";
require('../a.php');
echo "OK<br />"
a.php
echo "a.php ---> " . __DIR__ . "<br />";
require('./b.php');
echo "a is here<br />"
b.php
echo "b is here<br />"
When index.php
is called I got the following error:
index.php ---> D:\Programs\WampServer 2\www\temp\main
a.php ---> D:\Programs\WampServer 2\www\temp
Warning: require(./b.php) [function.require]: failed to open stream: No such file or directory in D:\Programs\WampServer 2\www\temp\a.php on line 5
Fatal error: require() [function.require]: Failed opening required './b.php' (include_path='.;C:\php5\pear') in D:\Programs\WampServer 2\www\temp\a.php on line 5
I have noticed that if I change
require('./b.php');
to
require('b.php');
Why is that ?
it works as expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用 include 或 require 时,您包含的文件将充当包含该文件的脚本的一部分。在这种情况下,文件
a.php
可能与b.php
位于同一目录中,但是当代码运行时,它在的上下文中运行>index.php
。如果您使用__FILE__
而不是__DIR__
,您会看到a.php
返回与index.php
相同的值code> 当它作为index.php
上的包含文件运行时。由于相对路径会根据文件的使用位置而变化,因此最好始终使用相对于服务器根目录的绝对路径。如果机器配置正常,将以
$_SERVER["DOCUMENT_ROOT"]
开头,然后添加应用程序路径和包含路径。在某些共享托管服务器上,您可能必须在某处对根进行硬编码(或将其添加到 .htaccess 文件中)。When you use include or require, the file that you include will act as if it is part of the script that included it. In this case, the file
a.php
might live in the same directory asb.php
, but when the code is running, it is running in the context ofindex.php
. If you had used__FILE__
rather than__DIR__
, you would see thata.php
returns the same value asindex.php
when it is running as an included file onindex.php
.Since the relative path changes depending on where files are used, it's always best to use an absolute path relative to the server root. If the machine is configured normally, that will start with
$_SERVER["DOCUMENT_ROOT"]
, then add the application path and the includes path. On some shared hosting servers, you might have to hard-code the root somewhere (or add it to the .htaccess file).使用
require('..\a.php');
PHP 手册 说
include
在 Windows 中使用反斜杠。编辑:
哎呀。我看错代码了。忽略这个答案。
Use
require('..\a.php');
The PHP manual says
include
uses backslashes for Windows.Edit:
Oops. I misread the code. Disregard this answer.