Page 1 of 1

relative path confusion

Posted: Wed May 05, 2010 12:24 pm
by phpBever
I'm wondering if I'm not understanding relative path correctly.

I've got a php file in http://www.site/ws (that is, /ws/file.php). I want to include a file at http://www.site/otherdirectory/toBeIncluded.php. I would think that this would work:

include("../otherdirectory/toBeIncluded.php");

But for some reason it chokes on that. If I enter the absolute path (http://www.site/otherdirectory/toBeIncluded.php) it works and if I create a shortcut in /ws it works.

Is there some reason I'm missing that include("../otherdirectory/toBeIncluded") doesn't work?

Thanks

Re: relative path confusion

Posted: Wed May 05, 2010 12:44 pm
by mikosiko
all depend of the relative location of your folder "otherdirectory" ...

did you try
include("/otherdirectory/toBeIncluded.php"); ?

Re: relative path confusion

Posted: Wed May 05, 2010 1:20 pm
by phpBever
I have concluded that the problem has something to do with where I place stuff in a webservice I'm trying to build using the WSDL_gen.php program rather than with some mistake in my relative address.

Thanks.

Re: relative path confusion

Posted: Wed May 05, 2010 4:23 pm
by cpetercarter
It is in general a not good idea to use relative paths in 'include' statements. It makes your code less portable. For example, if you decide to move file.php, you would need to hunt through it for every relative 'include', and change the path. It is better to define a constant to represent the absolute path from your server root to the directory where your include files are :

Code: Select all

define (INCLUDE_FILES, "/path/from/root/to/include/directory/");
and then

Code: Select all

include INCLUDE_FILES . "toBeIncluded.php";

Re: relative path confusion

Posted: Wed May 05, 2010 4:31 pm
by phpBever
Good point. Thanks.

Re: relative path confusion

Posted: Wed May 05, 2010 6:40 pm
by requinix
+1 to not being portable.

The other common solution is to use the DOCUMENT_ROOT.

Code: Select all

include $_SERVER["DOCUMENT_ROOT"] . "/otherdirectory/toBeIncluded.php";