8

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.

2
  • As a side note, "/all_fns.php" is an absolute path that will look for a file in the OS's root directory (on a non-Windows system) Commented Dec 23, 2009 at 19:42
  • On a Windows system as well. It will search in the current drive's root. Commented Dec 23, 2009 at 20:19

4 Answers 4

15

They are treated as relative to index.php.

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

__DIR__."/relative/file.php";
Sign up to request clarification or add additional context in comments.

5 Comments

This worked for me when I wanted a relative path. require_once('../bla.php'); would not work. require_once(__DIR__.'/../bla.php'); works.
@AlixAxel dirname(__FILE__) is needed on older PHP versions.
@ObsessiveFOSS: Yes, but the OP hasn't stated that he's using a very old PHP version, we can assume that he's using the latest stable release.
@AlixAxel so newest version is assumed?
@ObsessiveFOSS: Not the newest, but the latest stable one (give or take 6 months for server packages to catch up - launchpad.net/ubuntu/+source/php5), __DIR__ was stable since 30-Jun-2009. Either way, the answer and the comment still provide both options, I just think we shouldn't be advocating prehistoric code. But then again, SourceForge kept supporting PHP 3.X for a terribly long time.
2

They are relative to the getcwd().

1 Comment

Updated link: getcwd().
0

in index.php define some directories

define ( 'INCLUDES_DIR', dirname( __FILE__ ) );
define ( 'INCLUDES_DIR2', INCLUDES_DIR . '/some_directory' );

then in all the other files you can use INCLUDES_DIR

include( INCLUDES_DIR1 . 'file.php' );

Comments

0

Either Use

echo  in_get( "include_path") ;

Or open up your ini file and find out what your ini path is, and put your include file in THAT DIRECTORY (or one of those directories) and release yourself from worrying about absolute/relative include problems forever.

1 Comment

There isn't a function called in_get(). Do you mean ini_get()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.