【问题标题】:php include gives errorphp包含给出错误
【发布时间】:2012-09-16 22:11:01
【问题描述】:

我是 php 初学者,正在尝试制作一个 main.php 文件,其中包含页眉和页脚作为开始

header.php

<html>
<body>
<div class="header">
logo, navigation etc
</div>

footer.php

<div class="footer">
Footer content here
</div>
</body>
</html>

和主文件

<?php include "header.php"; ?>
<div class="main content">
<h1> header </h1>
<p> paragraph </p>
</div>
<?php include "footer.php"; ?>

错误:

Warning: include(header.php): failed to open stream: No such file or directory in - on line 1
Warning: include(): Failed opening 'header.php' for inclusion (include_path='.:') in - on line 1

Warning: include(footer.php): failed to open stream: No such file or directory in - on line 6
Warning: include(): Failed opening 'footer.php' for inclusion (include_path='.:') in - on line 6

所有文件都在同一个文件夹中,并在 XAMPP 上进行

【问题讨论】:

  • 哪个文件夹?父级为 htdocs 的文件夹?
  • .. 所以你的文件在 htdocs 中?
  • 你确定它们的名字是正确的并且不是'header.php.html'之类的吗?
  • Coda 请求使用 php 文件的权限似乎有些过时,但我确实检查了它们都是带有 noraml 扩展名的 header.php
  • 好吧,我希望我有一个更好的答案给你,但我无法重现你的问题。

标签: php include


【解决方案1】:

您包含的任何文件都必须在您的include_path 中。调用get_include_path查看当前值。

【讨论】:

  • include_path 已经包含.(这是脚本的当前目录),所以“没有吸文件”错误是神秘的。我的猜测是一个模糊的权限问题?
  • "这是脚本的当前目录" - 你确定吗?先试试 getcwd()
  • 我想它可能是......你可能有一些奇怪的 umask 并且正在创建不被所有人读取的文件。
【解决方案2】:

您应该将您的文件添加到同一个文件夹中,以便它们位于同一个父目录中,然后您可以使用“include”直接访问它们例如:在名为 Home 的文件夹中,如果我创建第三个文件,则有 2 个 php 文件我可以使用此代码“

【讨论】:

    【解决方案3】:

    如果页眉、页脚和主文件在同一个目录下,试试这个:

    include dirname(__FILE__) . '/header.php';
    

    【讨论】:

      猜你喜欢
      • 2013-06-08
      • 2010-12-28
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 2015-05-17
      相关资源
      最近更新 更多