模板的 require_once 问题

发布于 2024-07-27 12:50:05 字数 631 浏览 3 评论 0原文

我是 MVC 新手,我正在重新编写很多代码。

我有 master.php (索引),其中包含:

<?php require_once("header.php"); ?>

master.phpheader.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 技术交流群。

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

发布评论

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

评论(4

风吹短裙飘 2024-08-03 12:50:05

在您的评论中,您说您的代码是:

<?php require_once("header.php") or die; ?>

请尝试省略“或死”-> ist 不是必需的,因为如果找不到文件, require_once 将触发致命错误。

编辑:
我测试了它,省略“或死”,它会起作用。

编辑2:解释为什么会发生这种情况:
上面的代码也可以这样写:

require_once ( ("header.php") or die() );

因为 require_once 不是一个函数,而是 php 中的一条语句,所以大括号是可选的,php 将上面的内容解释为布尔表达式,其中字符串“header.php”被评估为布尔值“真的”。 True 被转换为字符串(结果为“1”),并且该字符串被传回 require 一次。

因此,上面的代码被解释为

require_once("1")

以下是如何逐步解释:

(("header.php") or die()) = (TRUE or die())
(TRUE or die()) = TRUE 
(string) TRUE = '1';

希望这会有所帮助;)

In your comment you say your code is:

<?php require_once("header.php") or die; ?>

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:

require_once ( ("header.php") or die() );

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

require_once("1")

Here is how it gets interpreted step by step:

(("header.php") or die()) = (TRUE or die())
(TRUE or die()) = TRUE 
(string) TRUE = '1';

hope this helps ;)

情归归情 2024-08-03 12:50:05

PHP 开始在根文件夹中查找“header.php”,但找不到它。 如果它与当前脚本位于同一目录中,则应该使用魔术常量 __DIR__

<?php require_once __DIR__ . '/header.php'; ?>

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__:

<?php require_once __DIR__ . '/header.php'; ?>
前事休说 2024-08-03 12:50:05

这可能是因为 master.php 被与 header.php 不在同一目录中的另一个脚本文件包含或需要。 也就是说,当你访问索引时,你知道哪个文件正在被调用/解析吗?

查看include()<的文档 /code>,这也适用于 require()

首先查看要包含的文件
对于每个 include_path 条目
相对于目前的工作
目录,然后在目录中
当前脚本的。 例如,如果你的
include_path 是库,当前
工作目录是/www/,你
包括 include/a.php 并且有
在该文件 b.php 中包含“b.php”
首先在 /www/libraries/ 中查找
然后在 /www/include/

更新

中解决一些评论:我没有说 include() 会更改 CWD。 我是说,如果 CWD 不是 master/header.php 所在的位置,那可能就是问题所在。

例如,假设您的 index.phpmaster.phpheader.php 位于不同的文件夹中; 它包含或需要 master.php 使用正确的路径。 现在,master.php 有这样的语句:

<?php require_once("header.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 as header.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 to require():

Files for including are first looked
for in each include_path entry
relative to the current working
directory, and then in the directory
of current script. E.g. if your
include_path is libraries, current
working directory is /www/, you
included include/a.php and there is
include "b.php" in that file, b.php
is first looked in /www/libraries/
and then in /www/include/

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 than master.php and header.php; it includes or requires master.php using the proper path to it. Now, master.php has the statement:

<?php require_once("header.php"); ?>

However, at this point the CWD does not contain header.php, since the CWD is that of index.php.

To fix this, you need to call require_once with the proper path from the CWD of the current script to header.php, NOT the path from master.php.

说好的呢 2024-08-03 12:50:05

您需要将第 15 行设置为如下:

<?php (require_once("header.php")) or die(); ?>

但是,由于未找到 header.php 时,require 是致命的,那么您应该具有:

<?php (include_once("header.php")) or die(); ?>

<?php require_once("header.php"); ?>

You need to have line 15 as this:

<?php (require_once("header.php")) or die(); ?>

However since when header.php is not found the require is fatal then you should have:

<?php (include_once("header.php")) or die(); ?>

or

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