简体   繁体   中英

how to include a php file from a folder

I'm trying to work with the include statement in php by including a php file where the footer is supposed to be. Yet, I am getting error messages saying
"include(includes/footer.php): failed to open stream: No such file or directory in "

OR

"Warning: include(): Failed opening 'includes/footer.php' for inclusion (include_path='.;C:\\php\\pear') " . I don't know if I am missing something or do I need to do something else when retrieving a file from a different folder? Here is code

include.php

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="../css/style.css"/>
  <title>include</title>
  <style type="text/css">
  .foot-item{width:33.33% min-height:120px; border-right:solid 1px white; }
  .foot-item:nth-last-child{ border-right:none;}
  .foot-item h3 a{color:#0099ff; text-decoration:none;}
  </style>
</head>
<body>
  <div class="contain">
    <article>
      <header><h1>include</h1></header>
      <section>
        <blockquote>The include statement includes and evaluates the specified file. </blockquote>
          <p>Default</p>
            <div id="result">Result</div>
            <?php

            ?>
          </section>

        </article>
      <?php  include('includes/footer.php');?>
      </div>
    </body>
    </html>

includes/footer.php

<footer>
  <div class="foot-item">
    <h3><a href="#">Recent Posts</a></h3>
    <ul><li>link<li><li>link<li><li>link<li></ul>
    </div>
    <div class="foot-item">
      <h3><a href="#">Free Newsletters</a></h3>
      <ul><li>link<li><li>link<li><li>link<li></ul>
      </div>
      <div class="foot-item">
        <h3><a href="#">Social Links</a></h3>
        <ul><li>link<li><li>link<li><li>link<li></ul>
        </div>
      </footer>

If your file structure is:

include.php
includes/
    footer.php

you can use:

<?php include('./includes/footer.php'); ?>

if file structure is different, write relative or absolute path to target file. For example if file structure:

code/
    include.php
includes/
    footer.php

you can write:

<?php include('../includes/footer.php'); ?>

PS '../' is folder up, './' is current folder, '../../' is two folder up.

Good luck!

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