2016-04-22 276 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

非常感謝先生! –

相關問題