include() referring to directory above

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
fredley
Forum Newbie
Posts: 7
Joined: Thu Oct 25, 2007 5:32 am

include() referring to directory above

Post by fredley »

I'm in http://www.tommedley.com/customers/index.php and I want to include http://www.tommedley.com/lib.php. I know I can't use absolute paths, what is the correct syntax to refer to the parent (or root) directory?

Thanks

Fred
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Read here:
http://www.php.net/manual/en/function.include.php
Look at the Contributed Note by slush puppie in the same page.
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Two things

Post by churt »

First, you can use include "../file.php"; to get to a file in the parent directory.

However, this can cause issues if you ever include a file that has an include. In that case you can use:

Code: Select all

include $_SERVER['DOCUMENT_ROOT'].'path to file';
This starts you from the web document root and you can just put the full path. It's still a relative reference but no issues when including a file that includes another file.
Post Reply