phpmeh
phpmeh

Reputation: 1792

Getting Relative Paths

Super noob question here...

The file source: www.mysite.com/blah/blah/customdir/ajax/folder/file.php

and I want to include a source:

www.mysite.com/blah/blah/customdir/include/func/functions.php

So I have tried:

require_once( '.../include/func/functions.php' );
require_once( '../include/func/functions.php' );
require_once( '././include/func/functions.php' );

and a ton of other variations. How do I get to it? Setting a top level path on every file isn't an option (that's what the function file is for...)

Each of the above is giving me an error: Warning: include(././include/func/functions.php) [function.include]: failed to open stream: No such file or directory in /home/mysite/public_html/mysite.com/customdir/ajax/folder/index.php on line 3

Warning: include(././include/func/functions.php) [function.include]: failed to open stream: No such file or directory in /home/mysite/public_html/mysite.com/customdir/ajax/folder/index.php on line 3

Warning: include() [function.include]: Failed opening '././include/func/functions.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/mysite/public_html/mysite.com/customdir/ajax/folder/index.php on line 3

Upvotes: 0

Views: 56

Answers (2)

Aerik
Aerik

Reputation: 2317

Are you running under apache? use getenv("DOCUMENT_ROOT") and set your path from there.

Upvotes: 0

Jason McCreary
Jason McCreary

Reputation: 72971

There are many ways to include a file. However, it seems you are attempting relative paths. In which case, try:

require_once '../../include/func/functions.php';

I encourage you to learn more about your options for including files. But in a nutshell, each ../ takes you back a directory. ./ represents the current directory. Otherwise, you can always use an absolute path or even (gulp) a hybrid.

Upvotes: 2

Related Questions