模板的 require_once 问题
我是 MVC 新手,我正在重新编写很多代码。
我有 master.php
(索引),其中包含:
<?php require_once("header.php"); ?>
master.php
和 header.php
都在同一个文件夹中,但我得到以下内容错误:
警告:require_once(1) [function.require-once]:无法打开流:第 15 行 /Users/MyName/Sites/MySite/Views/master.php 中没有此类文件或目录
致命错误:require_once() [function.require]:无法在第 15 行 /Users/MyName/Sites/MySite/Views/master.php 中打开所需的“1”(include_path='.:')
以前没有发生过这种情况。 (文件全部位于根文件夹中)。 我不明白问题是什么,因为它们位于同一个文件夹中 - 我不必返回 '../'
或引用任何其他文件夹。 我在这里缺少什么?
谢谢。
Im new to MVC and I'm re-doing a lot of my code.
I have master.php
(index) which has:
<?php require_once("header.php"); ?>
Both master.php
and header.php
are in the same folder yet I get the following error:
Warning: require_once(1) [function.require-once]: failed to open stream: No such file or directory in /Users/MyName/Sites/MySite/Views/master.php on line 15
Fatal error: require_once() [function.require]: Failed opening required '1' (include_path='.:') in /Users/MyName/Sites/MySite/Views/master.php on line 15
This did not happen before. (Files were all in root folder). I don't see what the problem is though, since they are in the same folder - I don't have to go back '../'
or reference any other folders. What am I missing here?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您的评论中,您说您的代码是:
请尝试省略“或死”-> ist 不是必需的,因为如果找不到文件, require_once 将触发致命错误。
编辑:
我测试了它,省略“或死”,它会起作用。
编辑2:解释为什么会发生这种情况:
上面的代码也可以这样写:
因为 require_once 不是一个函数,而是 php 中的一条语句,所以大括号是可选的,php 将上面的内容解释为布尔表达式,其中字符串“header.php”被评估为布尔值“真的”。 True 被转换为字符串(结果为“1”),并且该字符串被传回 require 一次。
因此,上面的代码被解释为
以下是如何逐步解释:
希望这会有所帮助;)
In your comment you say your code is:
please try leaving out the "or die" -> ist is not necessary, because require_once will trigger a fatal error if the file is not found.
Edit:
I tested it, leave out the "or die" and it will work.
Edit 2: Explanation why this happened:
The above code can also be written like this:
Because require_once is not a function, but a statement in php, the braces are optional, php interprets the above as a boolean expression, where the string "header.php" gets evaluated to the boolean "true". True gets cast to a string (resulting in "1"), and this string gets passed back to require once.
So the above code gets interpreted as
Here is how it gets interpreted step by step:
hope this helps ;)
PHP 开始在根文件夹中查找“header.php”,但找不到它。 如果它与当前脚本位于同一目录中,则应该使用魔术常量
__DIR__
:PHP starts looking for 'header.php' in your root folder and can't find it. If it is in the same directory as the current script, you should rather use the magic constant
__DIR__
:这可能是因为
master.php
被与header.php
不在同一目录中的另一个脚本文件包含或需要。 也就是说,当你访问索引时,你知道哪个文件正在被调用/解析吗?查看
include()<的文档 /code>,这也适用于
require()
:更新
中解决一些评论:我没有说 include() 会更改 CWD。 我是说,如果 CWD 不是 master/header.php 所在的位置,那可能就是问题所在。
例如,假设您的
index.php
与master.php
和header.php
位于不同的文件夹中; 它包含或需要master.php
使用正确的路径。 现在,master.php
有这样的语句:然而,此时 CWD 不包含
header.php
,因为 CWD 是index.php
。要解决此问题,您需要使用从当前脚本的 CWD 到
header.php
的正确路径调用require_once
,而不是master.php< 的路径/代码>。
It may be because
master.php
is being included or required by another script file that is not in the same directory asheader.php
. That is, when you visit the index, do you know which file is being invoked/parsed?Take a look at the documentation for
include()
, which also applies torequire()
:Update
To address some of the comments: I didn't say that include() would change the CWD. I'm saying that if the CWD is not the one where both master/header.php are located, that could be the issue.
For example, say you have
index.php
in a different folder thanmaster.php
andheader.php
; it includes or requiresmaster.php
using the proper path to it. Now,master.php
has the statement:However, at this point the CWD does not contain
header.php
, since the CWD is that ofindex.php
.To fix this, you need to call
require_once
with the proper path from the CWD of the current script toheader.php
, NOT the path frommaster.php
.您需要将第 15 行设置为如下:
但是,由于未找到 header.php 时,require 是致命的,那么您应该具有:
或
You need to have line 15 as this:
However since when header.php is not found the require is fatal then you should have:
or