1

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?

openfrog
  • 40,201
  • 65
  • 225
  • 373

4 Answers4

1

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
  • 442,112
  • 142
  • 972
  • 1,088
  • 1
    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 Dec 27 '09 at 20:12
  • You are absolutely right; forgot about that entirely. Edited my answer. – Pekka Dec 27 '09 at 20:26
0

Arg I'd like to comment but I can't...

@troelskn:

Include does NOT resolve to the include paths when you're using dirname(__ FILE __) because you are giving an absolute path to the include. The include paths are only searched when you DON'T give any path, only a filename (doesn't work with neither absolute nor relative paths).

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether.

http://php.net/manual/en/function.include.php

gaborous
  • 15,832
  • 10
  • 83
  • 102
0

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
  • 6,901
  • 3
  • 26
  • 30
0

This question is very old, but there still happens to be some confusion around this. Different functions having different relative paths is still in place in some points. In example the following may not work as expected:

if(is_file($target)){
    include $target;
}

is_file, is_dir, fopen - will use a path relative to the file which was requested by the HTTP request and will not be affected by set_include_path()

include, require - may have a different path assigned with set_include_path()

So more correctly the above code should look like:

if(is_file(get_include_path().'/'.$target)){
    include $target;
}
dadasign
  • 408
  • 4
  • 7