2016-04-22 246 views
0

,我发现了以下错误:致命错误:require_once():

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

我有下面的代码:位于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> 
+0

对不起,我有点新来的PHP - 我应该粘贴这个代码,因为它不会做任何事情 –

+1

其实你需要做'<?php require_once $ _SERVER [“DOCUMENT_ROOT”]。 'ecommerce1/inc/head.php';?>等等for others'或者<?php require_once'inc/head.php';?>等等为别人' –

+1

可能重复的[无法打开流:没有这样的文件或目录](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-或目录) –

回答

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';?> 

希望这会有所帮助。

+0

感谢兄弟它真的帮助!上帝祝福你! –

+1

请添加为什么你这么做。有些理由会更好。 –

+0

不客气,很高兴我能帮忙。 –

2

除非您明确更改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';?> 
+0

好吧不,我知道了..) –

1

有两种方法,包括在PHP文件

方法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');?> 

方法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');?> 
1

正如你应该知道什么时候使用include()以及何时使用require()初学者。您可以使用include()而不是require_once()

背后的原因是,如果require_once()无法加载文件,那么脚本执行将停在那里。如果您使用include()它只会引发错误并继续执行。

那么,何时使用require_once()超过include()

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

这个例子看看:

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

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

注:这是很好的做法是使用括号和治疗的功能,如功能。

+0

非常感谢先生! –

相关问题