The wonderful non-intuitive php include statement

The wonderful non-intuitive php include statement

am 30.08.2007 05:13:37 von Morlaath

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 = 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,
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.

Re: The wonderful non-intuitive php include statement

am 30.08.2007 05:45:42 von Jerry Stuckle

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 = 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,
> 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.
>

Some code would help...


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: The wonderful non-intuitive php include statement

am 30.08.2007 05:49:04 von Morlaath

On Aug 29, 10:45 pm, Jerry Stuckle wrote:
> Morla...@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 = 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,
> > 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.
>
> Some code would help...
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Uh ...what do you call this?
class foo {
private $xml;

function foo() {
$this->xml = simplexml_load_file('config/dbconfig.xml);
}

public function dbStuff() {
//do some db stuff
}

etc......

}

Re: The wonderful non-intuitive php include statement

am 30.08.2007 05:54:53 von Jerry Stuckle

Morlaath@gmail.com wrote:
> On Aug 29, 10:45 pm, Jerry Stuckle wrote:
>> Morla...@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 = 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,
>>> 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.
>> Some code would help...
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Uh ...what do you call this?
> class foo {
> private $xml;
>
> function foo() {
> $this->xml = simplexml_load_file('config/dbconfig.xml);
> }
>
> public function dbStuff() {
> //do some db stuff
> }
>
> etc......
>
> }
>

You didn't say *ANYTHING* about how you include it - which is your
*REAL* problem.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: The wonderful non-intuitive php include statement

am 30.08.2007 06:00:46 von Geoff Muldoon

In article <1188443617.322132.135940@22g2000hsm.googlegroups.com>,
says...
> 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 = simplexml_load_file('config/dbconfig.xml);

So the call by the simplexml_load_file function is to a file which is
addressed using a relative rather than absolute (starting with a / on *nix
or C: on windoze) file path.

> 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.

It's finding it as htdocs/config/dbconfig.xml.

> 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"

Because it's NOT finding it as htdocs/forum/config/dbconfig.xml. Why, coz
it ain't there!

> I tried using set_include_path(), which I could not get to work right,
> and I also tried to set 'include_path' in php.ini which also did not
> work.

Because include_path is used by the include() function, not by the
simplexml_load_file() function?

> PHP include seems to work very non-intuitive which is driving me
> nuts.

It's simply following standard file system rules.

> 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.

Find out what your $_SERVER['DOCUMENT_ROOT'] is - use a simple echo
statement if you need to, or read up about phpinfo() - it's likely it
will be //htdocs or C:\\\htdocs by the
sound of it.

The change the call in your function to:
simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/config/dbcon fig.xml);
or whatever absolute path you can use to properly locate the file.

Geoff M

Re: The wonderful non-intuitive php include statement

am 30.08.2007 06:06:49 von Morlaath

On Aug 29, 11:00 pm, Geoff Muldoon
wrote:
> In article <1188443617.322132.135...@22g2000hsm.googlegroups.com>,
> says...
>
> > 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 = simplexml_load_file('config/dbconfig.xml);
>
> So the call by the simplexml_load_file function is to a file which is
> addressed using a relative rather than absolute (starting with a / on *nix
> or C: on windoze) file path.
>
> > 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.
>
> It's finding it as htdocs/config/dbconfig.xml.
>
> > 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"
>
> Because it's NOT finding it as htdocs/forum/config/dbconfig.xml. Why, coz
> it ain't there!
>
> > I tried using set_include_path(), which I could not get to work right,
> > and I also tried to set 'include_path' in php.ini which also did not
> > work.
>
> Because include_path is used by the include() function, not by the
> simplexml_load_file() function?
>
> > PHP include seems to work very non-intuitive which is driving me
> > nuts.
>
> It's simply following standard file system rules.
>
> > 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.
>
> Find out what your $_SERVER['DOCUMENT_ROOT'] is - use a simple echo
> statement if you need to, or read up about phpinfo() - it's likely it
> will be //htdocs or C:\\\htdocs by the
> sound of it.
>
> The change the call in your function to:
> simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/config/dbcon fig.xml);
> or whatever absolute path you can use to properly locate the file.
>
> Geoff M

I forgot to write the fact that I was also having a problem with
'include()' for instance including a file with an include, which was
causing a similar problem. I am guessing I could also use
$_SERVER['DOCUMENT_ROOT'] for this as well, to use an absolute path
instead of relative.

Re: The wonderful non-intuitive php include statement

am 30.08.2007 06:14:43 von Morlaath

On Aug 29, 11:06 pm, Morla...@gmail.com wrote:
> On Aug 29, 11:00 pm, Geoff Muldoon
> wrote:
>
>
>
> > In article <1188443617.322132.135...@22g2000hsm.googlegroups.com>,
> > says...
>
> > > 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 = simplexml_load_file('config/dbconfig.xml);
>
> > So the call by the simplexml_load_file function is to a file which is
> > addressed using a relative rather than absolute (starting with a / on *nix
> > or C: on windoze) file path.
>
> > > 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.
>
> > It's finding it as htdocs/config/dbconfig.xml.
>
> > > 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"
>
> > Because it's NOT finding it as htdocs/forum/config/dbconfig.xml. Why, coz
> > it ain't there!
>
> > > I tried using set_include_path(), which I could not get to work right,
> > > and I also tried to set 'include_path' in php.ini which also did not
> > > work.
>
> > Because include_path is used by the include() function, not by the
> > simplexml_load_file() function?
>
> > > PHP include seems to work very non-intuitive which is driving me
> > > nuts.
>
> > It's simply following standard file system rules.
>
> > > 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.
>
> > Find out what your $_SERVER['DOCUMENT_ROOT'] is - use a simple echo
> > statement if you need to, or read up about phpinfo() - it's likely it
> > will be //htdocs or C:\\\htdocs by the
> > sound of it.
>
> > The change the call in your function to:
> > simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/config/dbcon fig.xml);
> > or whatever absolute path you can use to properly locate the file.
>
> > Geoff M
>
> I forgot to write the fact that I was also having a problem with
> 'include()' for instance including a file with an include, which was
> causing a similar problem. I am guessing I could also use
> $_SERVER['DOCUMENT_ROOT'] for this as well, to use an absolute path
> instead of relative.

Seems to be working .... thx ;)

Re: The wonderful non-intuitive php include statement

am 30.08.2007 18:32:08 von Richard Levasseur

On Aug 29, 9:14 pm, Morla...@gmail.com wrote:
> On Aug 29, 11:06 pm, Morla...@gmail.com wrote:
>
>
>
> > On Aug 29, 11:00 pm, Geoff Muldoon
> > wrote:
>
> > > In article <1188443617.322132.135...@22g2000hsm.googlegroups.com>,
> > > says...
>
> > > > 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 = simplexml_load_file('config/dbconfig.xml);
>
> > > So the call by the simplexml_load_file function is to a file which is
> > > addressed using a relative rather than absolute (starting with a / on *nix
> > > or C: on windoze) file path.
>
> > > > 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.
>
> > > It's finding it as htdocs/config/dbconfig.xml.
>
> > > > 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"
>
> > > Because it's NOT finding it as htdocs/forum/config/dbconfig.xml. Why, coz
> > > it ain't there!
>
> > > > I tried using set_include_path(), which I could not get to work right,
> > > > and I also tried to set 'include_path' in php.ini which also did not
> > > > work.
>
> > > Because include_path is used by the include() function, not by the
> > > simplexml_load_file() function?
>
> > > > PHP include seems to work very non-intuitive which is driving me
> > > > nuts.
>
> > > It's simply following standard file system rules.
>
> > > > 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.
>
> > > Find out what your $_SERVER['DOCUMENT_ROOT'] is - use a simple echo
> > > statement if you need to, or read up about phpinfo() - it's likely it
> > > will be //htdocs or C:\\\htdocs by the
> > > sound of it.
>
> > > The change the call in your function to:
> > > simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/config/dbcon fig.xml);
> > > or whatever absolute path you can use to properly locate the file.
>
> > > Geoff M
>
> > I forgot to write the fact that I was also having a problem with
> > 'include()' for instance including a file with an include, which was
> > causing a similar problem. I am guessing I could also use
> > $_SERVER['DOCUMENT_ROOT'] for this as well, to use an absolute path
> > instead of relative.
>
> Seems to be working .... thx ;)

just fyi: relative paths are relative to the executing script, similar
to the command line.

($ is the shell prompt)
/home/richardlev $ php myscript # paths are relative to /home/
richardlev
/home/richardlev $ php subdir/myscript $ paths are relative to /home/
richardlev
/home/richardlev/subdir $ php myscript # paths are relative to /home/
richardlev/subdir

in the context of apache and the web, the 'present working directory'
would be the path to the script, eg document root + url path
it can be a bit counter intuitive if you've been running everything
from the same directory and all the includes just seem to 'work'

if you want a file relative to the source file (eg, DbConfig.class
reads dbconfig.xml in the same dir), use the __FILE__ constant to
figure out the correct path.

finally, it would probably be best to pass down the config file path
from the controlling script, or establish some sort of application
root that it uses as a base path (using docroot is fine if you won't
have another instance in a subdirectory somewhere).

Re: The wonderful non-intuitive php include statement

am 02.09.2007 21:59:59 von luiheidsgoeroe

On Thu, 30 Aug 2007 05:13:37 +0200, 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/dbcon fig.xml);

or this (for relative to file):

$this->xml =3D simplexml_load_file(dirname(__FILE__).'/config/dbconfig.x=
ml);
-- =

Rik Wasmus