Page 1 of 1

Include-function in an included element

Posted: Thu Aug 02, 2012 1:12 pm
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.

Re: Include-function in an included element

Posted: Thu Aug 02, 2012 2:02 pm
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';

Re: Include-function in an included element

Posted: Thu Aug 02, 2012 4:52 pm
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.