Page 1 of 1

include problems with folders

Posted: Mon Oct 11, 2010 10:20 pm
by esel
Hello

I've got three files:

index.php

Code: Select all

<?php

echo "hello";
include ("system/page.php");
echo "bye"

?>
system/page.php

Code: Select all

<?php
include ("system/lib.php");
print_content();
?>
system/lib.php

Code: Select all

<?php
function print_content(){
   echo  "bla bla bla";
}
?>
When I now open the file index.php with my browser, everything is fine. But when I open the file system/page.php, there is an error that the file lib.php cant be found.
That means that php includes the files relative to the path of the first executed file. Is there a possibility to use paths relative to the file where I specified the include?

Re: include problems with folders

Posted: Mon Oct 11, 2010 10:30 pm
by Jonah Bron
Use the __FILE__ magic constant with dirname(). __FILE__ contains the absolute path to the script currently running. If you're running on 5.3 or later, use __DIR__ (without dirname()) instead.

http://php.net/manual/en/language.const ... efined.php
http://php.net/dirname

Code: Select all

include(dirname(__FILE__) . '/lib.php');
Or

Code: Select all

include(__DIR__ . '/lib.php');