Include-function in an included element

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
northernarc
Forum Newbie
Posts: 1
Joined: Thu Aug 02, 2012 1:01 pm

Include-function in an included element

Post by northernarc »

Hi, PHP-programmers out there.

I've been working on my website for a while now. My website contains a left column. That column is included on all pages using the <?php include("file.php); ?>-function, and that creates a problem on my website, because that column does also use the Include-function, and when I then navigate to files in other folders, I get the "No such file or directory in"-error, because the include-function does not accept absolute paths.

Does any of you know the answer of this?

Best regards,
Sebastian Stoll.
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Include-function in an included element

Post by tr0gd0rr »

Yes, includes are always relative to the file in which it is called, not the original file. You can add various paths to the include path, or you can use a path relative to the calling file: dirname(__FILE__) . '/../../to/my/file.php';
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Include-function in an included element

Post by requinix »

include is tricky. To be sure you're getting the right file every time always use an absolute path. dirname(__FILE__) or __DIR__ is best if you're thinking relative to the current file while $_SERVER["DOCUMENT_ROOT"] is best if you're thinking relative to the root of your website.
Post Reply