|
Posted by Rik Wasmus on 09/02/07 19:59
On Thu, 30 Aug 2007 05:13:37 +0200, <Morlaath@gmail.com> wrote:
> Ok .. I have some classes set up to do some database queries and what
> not. The database configuration is an XML file. Now I need to include
> this class in a few pages, in different directories. Here is an
> example of the class.
>
> class foo {
> private $xml;
>
> function foo() {
> $this->xml =3D simplexml_load_file('config/dbconfig.xml);
> }
>
> public function dbStuff() {
> //do some db stuff
> }
>
> etc......
> }
>
>
> I am using apache which has php set up as a module. This class (which
> resides 2 directories deep e.g htdocs/classes/foo) works fine in
> main.php which resides in htdocs. But if I include this class in
> another php file which is in a different directory .... say htdocs/
> forums I get this type of error:
>
> Warning: simplexml_load_file() [function.simplexml-load-file]: I/O
> warning : failed to load external entity "config/dbconfig.xml"
>
> I tried using set_include_path(), which I could not get to work right,=
set_include_path(get_include_path() . PATH_SEPARATOR . =
$_SERVER['DOCUMENT_ROOT']);
> and I also tried to set 'include_path' in php.ini which also did not
> work. PHP include seems to work very non-intuitive which is driving me=
> nuts. They really need to implement this a little better. I really
> don't see why the simplexml_load_file works in htdocs/classes/foo and
> not htdocs/forums.
Well, it's how you look at it. Being aware of what the current working =
directory is, or applying mechanisms so you don't have to is something t=
o =
learn. You are the one in control, and certainly with all kinds of files=
=
scattered about the place with possibly the same names I'd really hate f=
or =
PHP to 'guess' what I want. Important to remember is that the current =
working directory normally is the path of the script that got started by=
=
the request, and will NOT change in other included files. So a request =
that goes to http://www.example.com/index.php will havbe your =
'..../htdocs' as CWD for the while script (including includes :) ) for t=
he =
rest of the request, a request to http://www.example.com/foo/bar.php wil=
l =
have '..../htdocs/foo' as CWD. Use echo getcwd(); to understand more =
clearly what your scripts are doing.
That being said, for simple projects I'd normally use either something =
like this (for relative to root):
$this->xml =3D =
simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/config/dbconfig.xml);
or this (for relative to file):
$this->xml =3D simplexml_load_file(dirname(__FILE__).'/config/dbconfig.x=
ml);
-- =
Rik Wasmus
[Back to original message]
|