PHP include path not working

I am creating PHP pages on a Microsoft server. Up until now all of my files have been in the root folder and accessing include files from an includes folder. I have reached the point where I need to be a little more organized. I would like to move most (not all) of my php files into folders. I have setup a couple of test files and folders to try to use include files but they are not working. Here is what I have so far:

C:\inetpub\wwwroot\portal2\time_fix\time_fix.php (PHP file)
C:\inetpub\wwwroot\portal2\inc\header.php (Include file)

This line is in my php.ini file:

include_path = "C:\inetpub\wwwroot\portal2\inc"

This is the first line of my time_fix.php:

<?php include 'header.php';?>  

This is what I get when I try to run the file:

Warning: include(header.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\portal2\time_fix\time_fix.php on line 1

Warning: include(): Failed opening 'header.php' for inclusion (include_path='C:\inetpub\wwwroot\portal2\inc') in C:\inetpub\wwwroot\portal2\time_fix\time_fix.php on line 1

What is going on?
Robert FrancisDirector of Continuous ImprovementAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
There are two things going on.  In strings containing '\' in PHP, you need to double them up because the single '\' is an escape character.  Try...

include_path = "C:\\inetpub\\wwwroot\\portal2\\inc";

That is using 'absolute' addressing on the server.  You could use 'relative' addressing for files that are located under the web root folder.  Like...

include_path = "/portal2/inc";

Note that since the relative version uses forward slashes '/', they do not need to be doubled.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
quizwedgeCommented:
Instead of using include path, what if you use $_SERVER['DOCUMENT_ROOT']? For example, you could do the following:
<?php include $_SERVER['DOCUMENT_ROOT'] . '/header.php';?>

Open in new window

assuming that header.php is in the root directory of your site. If it is in the /inc directory then you would change that to /inc/header.php.
Ray PaseurCommented:
As far as  I can tell, PHP has some fuzzy areas with lots of configuration options, and this may be one of them.  PHP include(), like all PHP functions is documented in the online man page.  This is required reading for PHP developers.  Give yourself some time to read all of the docs and let it sink in - it's not as simple as it should be, and it may vary from one PHP installation to the next!  You probably want to read these, too:
http://php.net/manual/en/function.include-once.php
http://php.net/manual/en/ini.core.php#ini.include-path
http://php.net/manual/en/book.dir.php
http://php.net/manual/en/language.constants.predefined.php

You may find that your server produces a variable called $_SERVER['DOCUMENT_ROOT'].  Not all servers will produce this variable.  But in my experience if a server is setting it, you can rely on it with some certainty.  You may find that your include paths can be based on this variable.

You may want to include() scripts that you do not want others to be able to access from the browser address bar.  In this case, consider putting the included scripts outside the web root.  The man page link and user-contributed notes discuss this.

You can learn the existing working directory by installing the following PHP script.  Put it in your web root and also in some other directories, and navigate to it from the browser address bar.
<?php // getcwd.php
error_reporting(E_ALL);
$cwd = getcwd();
echo "CWD: $cwd" . PHP_EOL;

Open in new window

Requests to include() or require() scripts can be made relative to the current working directory.  There is some division of opinion among developers over whether relative directory paths are a good thing.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Ray PaseurCommented:
Further to Dave's comment, this is a useful predefined constant.  It is context-aware and will be correct no matter whether the scripts are run on Windows or Unix hosts.

DIRECTORY_SEPARATOR

http://php.net/manual/en/dir.constants.php
Robert FrancisDirector of Continuous ImprovementAuthor Commented:
quizwedge - this is what I get use use $_SERVER['DOCUMENT_ROOT']. A combination of \ and /

Warning: include(C:\inetpub\wwwroot/inc/header.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\portal2\time_fix\time_fix.php on line 1

Warning: include(): Failed opening 'C:\inetpub\wwwroot/inc/header.php' for inclusion (include_path='C:\inetpub\wwwroot\portal2\inc') in C:\inetpub\wwwroot\portal2\time_fix\time_fix.php on line 1
quizwedgeCommented:
Sorry, I was thinking of a Unix / Linux environment, but it looks like $_SERVER['DOCUMENT_ROOT'] is set to C:\inetpub\wwwroot/inc. Can you check your config file to see what DOCUMENT_ROOT is set to and then change it?

Were you able to run Ray's code (copied below) to get your current working directory?

<?php // getcwd.php
error_reporting(E_ALL);
$cwd = getcwd();
echo "CWD: $cwd" . PHP_EOL;

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.