Baros
Baros

Reputation: 1

How can I set an absolute path for include function in php above the working directory?

I am running a script from

/wp-content/themes/currenttheme/chat.php

I want to include in the above php another one located in

/forum/chat/index.php

The index.php includes its own files

I already tried

$root = $_SERVER['DOCUMENT_ROOT'];
include($root."/forum/chat/index.php");

but I get this error

Warning: require(D:/My Dropbox/xampp/htdocs/lib/custom.php) [function.require]: failed to open stream: No such file or directory in D:\My Dropbox\xampp\htdocs\forum\chat\index.php on line 17

Fatal error: require() [function.require]: Failed opening required 'D:/My Dropbox/xampp/htdocs/lib/custom.php' (include_path='.;\My Dropbox\xampp\php\PEAR') in D:\My Dropbox\xampp\htdocs\forum\chat\index.php on line 17

(the index.php also includes some files, but the /forum/chat is ommited somehow in the path)

then I tried

$path   = getcwd();
$myfile = "/forum/chat/index.php";
include ($path.$myfile);

and got this error:

Warning: include(D:\My Dropbox\xampp\htdocs\forum/forum/chat/index.php) [function.include]: failed to open stream: No such file or directory in D:\My Dropbox\xampp\htdocs\wp-content\themes\currenttheme\chat.php on line 24

Warning: include() [function.include]: Failed opening 'D:\My Dropbox\xampp\htdocs\forum/forum/chat/index.php' for inclusion (include_path='.;\My Dropbox\xampp\php\PEAR') in D:\My Dropbox\xampp\htdocs\wp-content\themes\currenttheme\chat.php on line 24

Upvotes: 0

Views: 1840

Answers (3)

Alpesh Panchal
Alpesh Panchal

Reputation: 1713

use this.

require_once(ABSPATH.'forum/chat/index.php');

here ABSPATH = WordPress physical root directory path with trailing slash

Upvotes: 0

symcbean
symcbean

Reputation: 48357

Something wrong with:

include('../../../forum/chat/index.php');

?

There are all sorts of reasons why the code you've published will fail.

C.

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157828

There is no problem with index.php. It is being included. The error message says about custom.php file

Just use the same $_SERVER['DOCUMENT_ROOT'] technique for the custom.php

you have to add /forum/chat manually as there is no path to be omitted

Upvotes: 1

Related Questions