【问题标题】:Fatal error: require_once():致命错误:require_once():
【发布时间】:2016-08-15 15:50:11
【问题描述】:

我收到以下错误:

警告:require_once(D:/xampp/htdocs/inc/head.php):打开失败 流:中没有这样的文件或目录 D:\xampp\htdocs\ecommerce1\index.php 在第 3 行

致命错误:require_once():需要打开失败 'D:/xampp/htdocs/inc/head.php' (include_path='.;D:\xampp\php\PEAR') 在 D:\xampp\htdocs\ecommerce1\index.php 在第 3 行

我有以下代码:位于D:\xampp\htdocs\ecommerce1 Index.php

<!--head-->
<?php $title="Gamer"?>
<?php require_once $_SERVER["DOCUMENT_ROOT"]. '/inc/head.php';?>
<?php require_once $_SERVER["DOCUMENT_ROOT"]. '/inc/menu.php';?>
<!--body of the page-->
<!--footer of the page-->
<?php require_once $_SERVER["DOCUMENT_ROOT"]. '/inc/footer.php';?>
`

这是位于D:\xampp\htdocs\ecommerce1\inchead.php

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?php print $title ?> </title>
    <link rel="stylesheet" type="text/css" href="/css/style.css">
    <script type="text/javascript" src="/jquery/jquery-1.12.3.min.js"></script>

</head>
<body>

【问题讨论】:

  • 对不起,我对 php 有点陌生——我应该把这段代码粘贴到那里,因为它什么都不做
  • 其实你需要要么&lt;?php require_once $_SERVER["DOCUMENT_ROOT"]. 'ecommerce1/inc/head.php';?&gt; and so on for others要么&lt;?php require_once 'inc/head.php';?&gt; and so on for others

标签: php pear fatal-error require-once


【解决方案1】:

除非您明确更改 Apache 的 httpd.conf 中的 DocumentRoot 设置,否则文档根默认位于 D:/xampp/htdocs 中。

所以你需要调用:

<?php require_once $_SERVER["DOCUMENT_ROOT"]. 'ecommerce1/inc/head.php';?>

而不是

<?php require_once $_SERVER["DOCUMENT_ROOT"]. '/inc/head.php';?>

【讨论】:

    【解决方案2】:

    在您的 index.php 中执行此操作。

    <?php $title="Gamer"?>
    <?php require_once 'inc/head.php';?>
    <?php require_once 'inc/menu.php';?>
    <!--body of the page-->
    <!--footer of the page-->
    <?php require_once 'inc/footer.php';?>
    

    希望这会有所帮助。

    【讨论】:

    • 请补充您这样做的原因。有些理由会更好。
    【解决方案3】:

    在php中包含文件有两种方法

    方法一: include()

    <?php $title= "Gamer"; ?>
    <?php include('inc/head.php');?>
    <?php include('inc/menu.php');?>
    <!--body of the page-->
    <!--footer of the page-->
    <?php include('inc/footer.php');?>
    

    方法二:require_once()

    <?php $title= "Gamer"; ?>
    <?php require_once('inc/head.php');?>
    <?php require_once('inc/menu.php');?>
    <!--body of the page-->
    <!--footer of the page-->
    <?php require_once('inc/footer.php');?>
    

    【讨论】:

      【解决方案4】:

      作为初学者,您应该知道何时使用include() 以及何时使用require()

      在您的情况下,请使用 include() 而不是 require_once()

      这背后的原因是,如果require_once() 加载文件失败,那么脚本执行就会停在那里。如果您使用include(),它只会抛出错误并继续执行。

      那么,什么时候使用require_once() 而不是include()

      在包含 PHP(或重要的服务器端)脚本时使用 require_once(),在包含类似模板的文件时使用 include()

      看看这个例子:

      <?php include_once('inc/head.php');?>
      <?php include_once('inc/menu.php');?>
      
      <!--if including a script-->
      <?php require_once('inc/footer.php');?>
      

      注意:最好使用括号并将这些函数视为函数。

      【讨论】:

        猜你喜欢
        • 2016-08-05
        • 2012-02-18
        • 2012-12-17
        • 2012-07-08
        • 1970-01-01
        • 2014-10-21
        • 2013-11-11
        • 2011-03-14
        • 2016-07-06
        相关资源
        最近更新 更多