Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Enhancing a Simple PHP Application Cleaning URLs with Subfolders Server Paths and Constants

Dan Neumann
PLUS
Dan Neumann
Courses Plus Student 10,318 Points

Adding include with relative link in php

I'm doing a code challenge which asks the following question:

There is a file located at htdocs/config/company.php on the server. We need to include that file to get access to some of information about the company. At the very top of the file below, before any of the HTML, include that company.php file using a relative server path.

The path of the file I'm working on is:

about/leadership/index.php

The code I added is:

<?php include("../config/company.php"); ?>

<html>
<head>
     <title>Leadership | Shirts 4 Mike</title>
</head>
<body>

    <h1>Leadership</h1>
    <p><strong>Owner:</strong> ????</p>
    <p><a href="/contact/">Contact</a></p>

</body>
</html>

This is the error I get:

It does not look like the config/company.php file is being included. Please try again.

Not sure what I'm doing wrong.

3 Answers

Ron McCranie
Ron McCranie
7,837 Points

htdocs is the root of the site. You can create a relative link from that point but not relative to the html file with ... like you had tried before.

<?php include('config/company.php'); ?>
<html>
<head>
     <title>Leadership | Shirts 4 Mike</title>
</head>
<body>

    <h1>Leadership</h1>
    <p><strong>Owner:</strong> <?= $owner; ?></p>
    <p><a href="/contact/">Contact</a></p>

</body>
</html>
Dan Neumann
Dan Neumann
Courses Plus Student 10,318 Points

Thanks - That worked but now I'm confused. Randy did links relative to the html he was working on. I'm not understanding why I couldn't do that.

Ron McCranie
Ron McCranie
7,837 Points

PHP needs the absolute location of the file (htdocs/config/company.php), not relative to the file the include is placed in (../config/company.php). Since PHP is run server side it doesn't know necessarily what file is running the code so it has no context to it's location in the site.

Dan Neumann
Dan Neumann
Courses Plus Student 10,318 Points

I did this:

<?php include("htdocs/config/company.php"); ?>

and got the same error. The question asks for a relative server path.