Link to home
Start Free TrialLog in
Avatar of Robert Francis
Robert Francis

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
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
Avatar of Robert Francis
Robert Francis

ASKER

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
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