0

I am working on a school project. My index works but when I click on 'Lab 1 Q1' located at labs/1/1.html it takes me to the page but my <?php include('/main-nav.php') ?> is not working. I also tried <?php include($_SERVER['DOCUMENT_ROOT']."/mobile-nav.php") ?> which did not work and I believe this is because I am currently using wamp so the 'root', I believe, is C:\wamp and inside there I have www/school/index.php.

How can I have a working file path not hard coded in?

1
  • first question: where is main-nav.php relative to the file you're including it in? Commented Jan 11, 2014 at 21:20

3 Answers 3

3

The include method is using server-side paths, so /main-nav.php is looking for the file in the root folder of your hard drive.

One option is to use a relative path:

include('../../main-nav.php'); // Go up two levels
Sign up to request clarification or add additional context in comments.

2 Comments

That would actually go back 2 directories from the current.
@RPM Yes it does! Which is what I point out in the comment. And if you look at the path to his labs/1/1.html page it looks like he may need to go up two levels.
1

Did you try with a relative path?

   <?php include('./main-nav.php') ?>

1 Comment

oh, you beat me to it!
0

For include path, have a look in include_path directive in your php.ini

you can also try a

<?php phpinfo() ?>

and search the "include_path" directive

"/" have no sense in a Windows OS.

6 Comments

Actually / works great on Windows. Assuming you really do want to start at the root hard drive folder.
It's ok but which root if you have 2 harddrives for example. It work but you're not really sure of the result.
You can be absolutely sure of the result. I run a number of production PHP apps on Windows servers with paths this way.
I just want to say that if the document_root is on C then root is the root of C and if the document_root is on D then root id D:\. Imagine you have two servers with a DOCUMENT_ROOT on C for the first and D for the second, you cannot be sure of the result if you search "/WINDOWS/SYSTEM32" for exemple. No ?
Your statement was that / has "no sense" on Windows. That simply isn't true. Of course you need to know where your document root is at. And why in the world would you be including something inside system32? We're talking about including a PHP script, that is inside the same document root as the PHP script doing the include. Cross drive access isn't an issue at all here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.