Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - with require_once/include/require, the path is relative to what?

Tags:

path

php

when on "index.php", I do require_once("/all_fns.php").

"all_fns.php" itself requires files with paths relative to all_fns.php (itself).

My question is, should I write the paths on all_fns relative to all_fns.php or to index.php?

This is all very confusing to me and wanted to get it straight.

like image 546
Gal Avatar asked Dec 23 '09 19:12

Gal


People also ask

What does Require_once mean in PHP?

Definition and Usage The require_once keyword is used to embed PHP code from another file. If the file is not found, a fatal error is thrown and the program stops. If the file was already included previously, this statement will not include it again.

What is difference between require and require_once in PHP?

The require() function is used to include a PHP file into another irrespective of whether the file is included before or not. The require_once() will first check whether a file is already included or not and if it is already included then it will not include it again.

What is include and require in PHP?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

What is absolute and relative path in PHP?

Absolute and Relative PathsAn absolute path refers to a file on the Internet using its full URL, e.g. "http://www.uvsc.edu/disted/php/webct/itr/index.php" A relative path assumes that the file is on the current server, e.g. "php/webct/itr/index. php".


2 Answers

They are treated as relative to index.php.

If you need to reference a file relative to the include, use

__DIR__."/relative/file.php";
like image 75
Pekka Avatar answered Oct 07 '22 20:10

Pekka


They are relative to the getcwd().

like image 34
Alix Axel Avatar answered Oct 07 '22 19:10

Alix Axel