include problems with folders

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
esel
Forum Newbie
Posts: 1
Joined: Mon Oct 11, 2010 10:06 pm

include problems with folders

Post 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?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: include problems with folders

Post 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');
Post Reply