Where are we?
You saw how the
This lesson’s goals
By the end of this lesson, you should:
- Know how to use PHP to insert a file into the HTML output.
- Know how to use a file path to insert the same file in pages that are spread out across a directory tree.
PHP has several statements that insert files:
require_once. We’ll just talk about one of them:
Suppose we create a file that contains HTML to show a footer for every page on a Web site. Here it is:
<div id="footer"> Copyright © 2010 | All rights reserved </div>
Figure 1. Footer code.
This is not a complete HTML page, just a fragment of HTML. It wouldn’t work by itself. Let’s call the file
.inc reminds us that the file is meant to be included in another file.
Here’s the PHP page
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Including a footer</title> </head> <body> <h1>Hi there!</h1> <p>I am a page. Aren't I wonderful?</p> <?php require 'footer.inc'; ?> </body> </html>
Figure 2. Including a footer
require statement reads the file it’s given, and inserts it. Any PHP in the included file is executed. If the PHP interpreter can’t find the file, it shows an error message.
You can try it. Make sure you have a look at the HTML source. As far as the browser knows, the content of
footer.inc was typed directly into
Inserting files from other directories
The code in Figure 2 is in a file called
include-footer.php. It has the line:
There is no directory given, just a file name. So where will the PHP interpreter look for
The answer: in the same directory as
include-footer.php. So if
include-footer.php is in
/my/directory/of/doom/, then the PHP interpreter will look for
When you’re using dynamic Web templates for real, this won’t work very well. Suppose you were creating a site with this structure:
Figure 3. Site structure.
The file at
/index.php is the home page of the site. The file at
/articles/index.php shows a list of articles. There are other things like blog entries, products, and so on, but we don’t need to consider them here.
Suppose you put this line into both
You would need one
/index.php and another for
Figure 4. Duplicate footer files.
But what’s the point of that? If we wanted to change the footer text across the entire site, we’d have to change all of the
What we really want is for both
/articles/index.php to refer to the same
Figure 5. Just one footer file.
footer.inc is in a directory called
library. PHP developers often move shared files to a directory like this, and call it
common, or something like that.
But we have to change the
require statements to make this work. Here is the require statement from
Here is the one for
.. means “go up a level.”
To brush up on how to use paths to navigate between directories, see the discussion of absolute, relative, and root relative links.
Let’s see how this would work in an entire Web site.
Let’s create a simple site called The Dog Site. It has a home page and two sections: dog profiles, and articles.
Figure 6. Site overview.
Each of the two sections of the site – dog profiles and articles – has a main page. Each one is linked from the home page.
Here is the home page:
Figure 7. Home page.
Here is the main page for the dog profile section:
Figure 8. Dog profile main page.
Here is the main page for the articles section:
Figure 9. Articles main page.
You can try the site.
Let’s create the directory tree for the site, that is, the directories on the server that all the files will go into. We’ll make a directory for each section of the site: dog profiles and articles.
The header is the same on every page, so let’s pull that out and put it in its own file. Here’s the code:
<!-- Header file, to be included into every page. --> <h1>The Dog Site</h1> <h2>Bringing you happiness since last week</h2> <hr>
Figure 10. HTML for the header.
Let’s put the code into the file
header.inc, and put the file into a library directory.
Here is the directory tree so far.
Figure 11. Initial tree.
The home page is
index.php. Recall that if a browser sends a URL to a Web server, and the URL does not have a file name, then the browser uses a default name. In ClientCore, we used
index.html as the default.
index.php can be the default as well.
So if a browser asks for:
the server will run:
Let’s see what the tree looks like when all the files are added:
Figure 12. Complete tree.
All of the PHP files are going to have a
require statement, to insert
header.inc. But since the PHP files are in different directories, they will use different paths to navigate to
header.inc. Here are the paths for some of the files.
Figure 13. Paths in some of the files.
Exercise: The rest of the paths
Complete figure 13. Give the path in the
require statement for every PHP file.
You can check your solution by downloading a zip file of the site. Expand the zip file to see the PHP code.
(Log in to enter your solution to this exercise.)
Exercise: Inserting files
Download and expand this zip file. It will create a directory tree like this:
Figure 1. Directory tree
require statements to create a page that looks like this:
Figure 2. Output
Use only relative file paths.
Upload the entire tree to your server. Put the URL below.
You can see my solution, but try it yourself first.
(Log in to enter your solution to this exercise.)
In this lesson, you learned:
- How to use PHP to insert a file into the HTML output, using the
- How to use a file path to insert the same file in pages that are spread out across a directory tree.
But there’s a problem. It’s easy to break links to images and other resources. Let’s fix that in the next lesson.