PHP:包含和文件夹

问题描述 投票:1回答:5

我有一个小型的PHP网站,其中包含我的大部分页面形式:

<?php include("heading.php"); ?>

<!-- Content of the page -->

<?php include("footing.php"); ?>

“footing”包含一些东西放在每个文件的末尾,并“标题”很多东西(包括另一个包括菜单)。问题是:我开始有很多php文件沙我想把它们中的一些放在文件夹中。直接的方法是将文件夹中文件的代码更改为:

<?php include("../heading.php"); ?>

<!-- Content of the page -->

<?php include("../footing.php"); ?>

但它不起作用,因为包含文字复制代码而不是在原始文件的文件夹中执行它,因此除非我在新文件夹中复制这些文件,否则将找不到heading.php中的任何include和css。

php include folder
5个回答
2
投票

修改heading.php,使其加载的文件的路径是绝对的。例如,如果从css/style.css加载CSS文件,请使用/css/style.csshttp://example.com/css/style.css


2
投票

您还可以在php.ini中或直接在代码中编辑包含路径:

//Must be called before every include, if not stated in your php.ini
$PATH=get_include_path();
set_include_path($PATH.":/absolute/path/to/your/include/folder/");

然后,即使您在子目录中,也可以使用这种方式:

<?php
  include "heading.php"; //no brackets
  include "whatever.php";
?>

1
投票

听起来你只是忘记了目录结构中的位置。使用../将你带回一个级别并使用./(或只是/)将你带回到根目录并且../../将你带回两个级别。

诀窍是知道您在目录结构中的位置以及要包含的文件所在的位置。如果你包含或链接到css文件夹中的文件,但是你在一个位于inc文件夹中的文件中,则必须返回到root,然后返回到css,这样你就可以放入/css/filename.ext

但是,如果您在fonts / glyphicons文件夹中并想要链接到fonts / font-awesome文件夹中的字体,则可以使用../font-awesome。像泥一样清楚?

我所做的是创建三个(或更多)文件夹。这有助于保持各种文件的组织和缩写字母,即:

  • css(用于样式表,如bootstrap.css,style.css等)
  • img(用于图像,图形,图标等)
  • font(用于字体,glyphicons,font-awesome等)
  • inc(对于includes /'snippets',如你的heading.php和footing.php)
  • js(适用于javascripts,如html5shiv.js,bootstrap.js等)

然后在我的heading.html文件中(我使用html作为页眉和页脚)我包括一些像这样的项目:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title><?php echo $page_title; ?></title>
        <meta name="Description" content="<?php echo $page_description; ?>">
        <meta name="Keywords" content="<?php echo $page_keywords; ?>">
        <meta name="Author" content="<?php echo $author_name; ?>">
        <meta name="Viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <link href="/css/quicksite-redsand.css" rel="stylesheet">
        <link href="/img/mm.png" type="image/x-icon" rel="shortcut icon">
        <!--[if lt IE 9]><script src="/js/html5shiv.js"></script>
        <script src="/js/respond.min.js"></script><![endif]-->
    </head>
    <body>

然后在我的页面中,例如index.php(主页),我为该特定页面设置了一些参数,并包括一些片段,如标题,菜单,其他正文文本项和页脚,如下所示:

    <?php 
    $page_name = 'Homepage';
    $page_author = 'My Name...';
    $page_keywords = 'home, home page, homepage';
    $page_description = 'This is the home page for the ...';
    $active_home = 'class="active"';
    $select_home = 'class="selected"';
    include ('inc/config.php');  //common to all pages
    include ('inc/header.html'); //common to all pages
    include ('inc/menus.html');  //common to all pages
    include ('inc/banner.html');
    include ('inc/review.html');
    include ('inc/footer.html'); //common to all pages
    ?>

因为在你的index.php和你的大部分页面中你都处于根级别,所以在主目录中,你只需要没有/的inc。看起来PHP不喜欢不必要的,因为它在我尝试时给了我一个错误...

我知道这是一个冗长的回答,但我想我会说明更好的理解。


0
投票

好吧,不是严格地回答你的问题,但尝试以另一种方式做,有一个包含页眉和页脚的布局文件,并动态包含主要内容。

<html>
    <head></head>
    <body>
        etc...

        <?php include_once '/path/to/content.php'; ?>

        etc...

   </body>
</html>

0
投票

我不确定我是否理解你的问题,但我有以下答案我认为你应该使用相对的css路径到你的网站地址,例如,如果我们有一个css文件,它应该是这样的

<link ...src="/styles/somecss.css">

所以,无论你把它放在你的应用程序中,这将采取路径http://wesiteaddres/style/somecss.css

并且您可以将include路径设置为您想要的任何目录set_include_path();

希望这会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.