PHP Includes 和 Require 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21796707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:26:55  来源:igfitidea点击:

PHP Includes and Require not working

phpinclude-path

提问by Tauciokas

I know this question has been asked before, but even after reading other posts I can't figure out whats going on...

我知道之前有人问过这个问题,但即使在阅读其他帖子后我也无法弄清楚发生了什么......

Please have a look at my code structure and see what am I doing wrong..

请看看我的代码结构,看看我做错了什么..

Folder structure:

文件夹结构:

index.php

索引.php

(FOLDER - Includes) header.php

(文件夹 - 包括)header.php

(FOLDER - browse) blog.php , ecommerce.php, other.php

(文件夹 - 浏览) blog.php 、 ecommerce.php 、 other.php

(FOLDER - core) init.php

(文件夹 - 核心)init.php

I'm trying to include Header.php from includes folder in blog.php in browse folder. Below is my header.php code.

我正在尝试将 Header.php 从包含文件夹中的 blog.php 包含在浏览文件夹中。下面是我的 header.php 代码。

Header file located in Includes folder

位于 Includes 文件夹中的头文件

    <!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
<head>

    <!-- Basic Page Needs
  ================================================== -->
    <meta charset="utf-8">
    <title>Web Awwards | Web Gallery | Submit your site</title>
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Mobile Specific Metas
  ================================================== -->
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <!-- CSS
  ================================================== -->
    <link rel="stylesheet" type="text/css" href="../css/base.css">
    <link rel="stylesheet" type="text/css" href="../css/skeleton.css">
    <link rel="stylesheet" type="text/css" href="../css/layout.css">
    <link rel="stylesheet" type="text/css" href="../css/styles.css">
    <link rel="stylesheet" type="text/css" href="../css/menu.css">
    <link rel="stylesheet" type="text/css" href="../css/submission.css">

    <!--[if lt IE 9]>
        <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Favicons
    ================================================== -->
    <link rel="shortcut icon" href="images/favicon.ico">
    <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
    <link rel="apple-touch-icon" sizes="72x72" href="images/apple-touch-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png">

</head>
<body>
<?php
    require_once('core/init.php');
?>
<!-- Menu Horizontal -->
<div class="horizontal">
    <div class="webName">

and Browse folder blog.php file code:

并浏览文件夹 blog.php 文件代码:

<?php
    include './includes/header.php';
?>
<!-- TODO PHP Scripts -->
<div class="category nine columns">
    <div class="category-img">
        <img src="css/img/webImg/screen1.png" alt="Site Description"/>
    </div>
    <h3> Web Name </h3>
    <h5> Designed By: Tautvydas Slegaitis </h5>
    <h7> Designers url </h7>
    <div class="vote">
        <h4> Vote for my site </h4>
        </br>
        <a href="#"> Love it </a>
        <a href="#"> Nope, Not for me </a>
    </div>
</div>

</div><!-- Close Container Div -->
<div class="footer">


</div>



</body>
</html>

But it is not working. I dont know why,... Please please help me sort this out its killing me for the past 2 days. Coda 2 is showing me an error

但它不起作用。我不知道为什么,...请帮我解决过去 2 天它让我丧命的问题。Coda 2 显示一个错误

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

" 警告:include(./includes/header.php):无法打开流:第 2 行中没有这样的文件或目录警告:include():无法打开 './includes/header.php' 以进行包含(include_path ='.:') in - 第 2 行“

init.php code:

init.php 代码:

<?php
session_start();

$GLOBALS['config'] = array(
    'mysql' => array(
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'root',
        'db' => 'webAwwards'
    ),
    'remember' => array(
        'cookie_name' => 'hash',
        'cookie_expire' => 604800
    ),
    'session' => array(
        'session_name' => 'user'

    )
);
// TODO Remove parenthesis in require once if doesnt work
spl_autoload_register(function($class) {
    require_once('classes/' . $class . '.php');
});

require_once('/functions/sanitise.php');

?>

回答by Vainglory07

try :

尝试 :

include('../test/header.php');

it works fine in me.

它在我身上工作得很好。

Edit: and if not on your case, try to correct the path of your init.php:

Edit:如果不是您的情况,请尝试更正您的路径init.php

require_once('../core/init.php');

回答by LUCAS NANTES

You could try something like:

你可以尝试这样的事情:

include $_SERVER['DOCUMENT_ROOT'] .'*/project_name_if_needed*/test/header/';

I don't know if this is recommended, so use with caution.

我不知道这是否值得推荐,所以请谨慎使用。