简体   繁体   中英

Fatal error: require_once():

I'm getting the following error:

Warning: require_once(D:/xampp/htdocs/inc/head.php): failed to open stream: No such file or directory in D:\\xampp\\htdocs\\ecommerce1\\index.php on line 3

Fatal error: require_once(): Failed opening required 'D:/xampp/htdocs/inc/head.php' (include_path='.;D:\\xampp\\php\\PEAR') in D:\\xampp\\htdocs\\ecommerce1\\index.php on line 3

I have the following code : located in 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';?>
`

This is the head.php which is located in D:\\xampp\\htdocs\\ecommerce1\\inc

    <!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>

Unless you explicitly change the DocumentRoot setting in Apache's httpd.conf , the Document Root is by default in D:/xampp/htdocs .

So you need to call:

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

instead of

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

Do this in your 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';?>

Hope this helps.

There are two methods to include files in php

Method 1: 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');?>

Method 2: 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');?>

As a beginner you should know when to use include() and when to use require() .

In your case, use include() instead of require_once() .

The reason behind this is that if the require_once() fails to load a file, then the script execution would stop right there. If you use include() it would just throw an error and would continue execution.

So, when to use require_once() over include() ?

Use require_once() when including PHP(or important server-side) scripts and include() when including template-like files.

Take a look at this example:

<?php include_once('inc/head.php');?>
<?php include_once('inc/menu.php');?>

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

Note: It's good practice to use brackets and treat those functions like functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM