You are here: Re: The wonderful non-intuitive php include statement « PHP Programming Language « IT news, forums, messages
Re: The wonderful non-intuitive php include statement

Posted by Geoff Muldoon on 08/30/07 04:00

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 /<maybe_something>/htdocs or C:\\<maybe_something>\htdocs by the
sound of it.

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

Geoff M

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация