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

Martina Carrington
Martina Carrington
15,754 Points

some reason i can't pass Task 2/3 stage 2 cleaning URLS with subfolders <?php include($_SERVER["DOCUMENT_ROOT"]."../../

i have try <?php include($_SERVER["DOCUMENT_ROOT"]."../../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> and i can't getting error's

about/leadership/index.php
<?php include($_SERVER["DOCUMENT_ROOT"]."../../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>

3 Answers

Thiago van Dieten
Thiago van Dieten
16,639 Points

Honestly, what I think you are doing is correct, that would be the way to include that file relatively.

To debug what you are doing anyway, I did a var_dump on $_SERVER['DOCUMENT_ROOT'] and that returned an empty string, so we have no use for that. So then you are left with:

include("../../config/company.php")

Which will fail, saying: PHP Warning: include(../../config/company.php): failed to open stream: No such file or directory in about/leadership/index.php on line 3 PHP Warning: include(): Failed opening '../../config/company.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in about/leadership/index.php on line 3

When you try to include that file like this however

include('config/company.php');

Treehouse will say you're correct and lets you progress. Which is weird, because the full path would be htdocs/about/leadership/config/company.php . So yeah, beats me!

Jayden Spring
Jayden Spring
8,625 Points

DOCUMENT_ROOT outputs the doc routes absolute path of the current working directory which the script is being executed in.

Therefore you are appending a string to an absolute path which if you printed the path you are trying to form to the screen it would be something like; /var/www/html/../../company/config.php

That path would cause an error - it's in accessible. If you are trying to access something in the current path you can use either for your scenario:

  • include('myscript.inc') - would look in current directory
    • include('./myscript.inc') - ./ denotes in current directory
    • include($_SERVER["DOCUMENT_ROOT"{ . "Myscript.inc")
Thiago van Dieten
Thiago van Dieten
16,639 Points

No problem, don't forget to pick the answer that helped you. ;)