Monday, May 4, 2009

PHP include path

How to set include path



Include path separator differs from one operating system to another. For Windows path separator is ; and for unix separator is :. So how do you make your code create the right include path regardless of your OS?

First, you detect server's OS:

if (stristr(PHP_OS, 'WIN'))
{
$separator = ';';
}
else {
$separator = ':';
}

Then you set your classes path, for example:
$yourpath = './classes';

And then you add this path to include path:

ini_set('include_path', ini_get('include_path') . $separator . $yourpath);

This way, either your server is Windows or a Unix system, it will always include your path using the right separator character, and you don't have to change the code everytime you switch to another OS.

1 comment:

Rodrigo Primo said...

Another option is to use the PATH_SEPARATOR constant.