Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Trevor Wood
Trevor Wood
17,828 Points

PHP relative path include, code challenge

The Challenge: 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.

My answer:

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

The response: Bummer! It does not look like the config/company.php file is being included. Please try again.

I've also tried "company.php" "/company.php" "config/company.php" "/config/company.php" however none seem to work.

Can anybody help me with this code challenge?

4 Answers

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

The index.php is currently in the htdocs/about/leadership directory. The company.php file is located in htdocs/config/company.php , so you have to go back two folders before you can access the config folder.

Trevor Wood
Trevor Wood
17,828 Points

Thanks Harpreet, worked perfectly

I had the same problem, the "workdir" is not set correctly, using "include('../../workdir/config/company.php')" resolves the issue.

Abdulwahab Alansari
Abdulwahab Alansari
15,151 Points

Great, thanks, now I am able to pass the challenge. However, I am wondering where did "workdir" come from?

Kevin May
Kevin May
8,904 Points

I get so tired of these stupid challenges, taking way too much time and not getting anything out of them, I agree with Abdul that there is no mention of workdir anywhere, so I'm not sure how in the hell you're supposed to know this.

Andrew Breslin
Andrew Breslin
10,177 Points

Well, now that I've given up and looked up the answer, I can at least comfortably say "There is absolutely now way I could have gotten that one."

workdir? Where did that come from? It's nowhere in the code challenge.

Usually these challenges do a great job of forcing me to really try to understand what's going on and although it's frustrating at first, it usually eventually makes sense. I think whoever put this one together just made a mistake in wording the question.

Thanks!

Giacomo Alberini
Giacomo Alberini
9,548 Points

Hi guys!

I have to say that I am really surprised by the fact that this code does not work for me!

It was my first choice when I did this challenge but the answer is always the same: "Bummer! It does not look like the config/company.php file is being included. Please try again."

I even tried to use the $_SERVER['DOCUMENT_ROOT'] (with single and double quotes) to retrive the root of the document but I found that variable to be empty (I used var_dump() to discover its content).

No luck with require, require_once, include_once and Root-relative path.

In the preview the answer is always the same: "PHP Warning: include(../../config/company.php): failed to open stream: No such file or directory in /workdir/about/leadership/index.php on line 2 PHP Warning: include(): Failed opening '../../config/company.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /workdir/about/leadership/index.php on line 2"

It seeems that the PHP is not able to go backward to the right folder and it just searches for the file in the current directory (it's really weird, but I think it's looking for the file ../../config/company.php in the /about/leadership/ directory)

Can anybody help me? Is this an error of mine or is the system broken for some reason?

Thank you!

Giacomo

Giacomo Alberini
Giacomo Alberini
9,548 Points

This is the link to the Code Challenge which is the subject of this thread.

If you are able to find why it's not working, you'll be my next hero :)

http://teamtreehouse.com/library/enhancing-a-simple-php-application/cleaning-urls-with-subfolders/server-paths-and-constants

Thank you!

Giacomo

There might be a problem with the code challenge.

The code in the "best answer" here should be the right code.

I found a recent thread with a similar problem: https://teamtreehouse.com/forum/problem-with-code-challenge-23

There's a workaround for it so that you can pass the challenge.

I will tag the staff in that new thread.