tags:

views:

113

answers:

2

I slightly remember from age old PHP days (years ago) that different functions wanted to have different paths. I mean...starting from different points. Some were relative, others absolute, etc.

How about fopen? Is that the same thing like require? Same path in same situation?

+1  A: 

Paths are always relative to the initial script's location, even if the parser is going through an include that resides in a different directory.

To reliably work with paths relative to the current file, use

dirname(__FILE__)

or in PHP 5

__DIR__

in addition, as @troelskn points out below, require and include search the include_path.

Pekka
php will search the include-path, so this is not entirely true. However, the include-path usually includes `.` (current directory), which is the working directory of the runtime. In a web server context, this is initialised to the location of the initial script. See: [`chdir`](http://www.php.net/manual/en/function.chdir.php) and [`getcwd`](http://www.php.net/manual/en/function.getcwd.php). `fopen` doesn't use include-path.
troelskn
You are absolutely right; forgot about that entirely. Edited my answer.
Pekka
A: 

include and require will look for a file relative to the setting given to it in php.ini first and foremost.

Say your ini file's include path entry is:

include_path = "var/www/includes;/var/www/PEAR"

Then in your scripts, no matter where in your document tree they are, eg

/var/www/html/website1/miles/down/in/folders/index.php

you just do this to include a file:

include 'settings.php' ;

As long as settings.php is one of the include_path folders, it will be included, then you can stop worrying about relative/absolute path relationships.

This setting can be altered in .htaccess files and per-file using ini_set() if you want too.

More on this: http://php.net/manual/en/function.set-include-path.php http://www.modwest.com/help/kb.phtml?cat=5&qid=98

or google for "include_path php"

Cups