philosopherdog Posted May 13, 2012 Share Posted May 13, 2012 Hi, I'm pretty new to php and need a bit of basic help. I have a header and footer include file in a "views" folder. The header itself has a function that's included from another directory. Now when I try to include the header in by index.php in the root it throws and error saying it can't find my function include. Obviously the include directory is relative to the file it's included in. So, what's the trick to fixing this problem? And what's the lesson for future projects? Thanx in advance. Quote Link to comment https://forums.phpfreaks.com/topic/262486-proper-way-to-do-includes-for-small-site/ Share on other sites More sharing options...
requinix Posted May 13, 2012 Share Posted May 13, 2012 Obviously the include directory is relative to the file it's included in. No: it's relative to the directory of the first executed file. If that first file includes a file in some other directory, the include path will not "update" to reflect that other directory. Use absolute file paths to include files. // relative to the current file require_once __DIR__ . "/path/../to/../function.php"; // PHP 5.3+ require_once dirname(__FILE__) . "/path/../to/../function.php"; // relative to the web root require_once $_SERVER["DOCUMENT_ROOT"] . "/path/to/function.php"; Quote Link to comment https://forums.phpfreaks.com/topic/262486-proper-way-to-do-includes-for-small-site/#findComment-1345187 Share on other sites More sharing options...
philosopherdog Posted May 13, 2012 Author Share Posted May 13, 2012 Yes, my mistake. Thanx for the quick reply. Just so I understand what's happening here __DIR__ grabs the current directory. So, let's say my header.php is in the views folder. require_once __DIR__ . "/path/../to/../header.php"; will append the current directory to this path. So, let's say the current directory is forum/views. So it would render as require_once 'forum/views/header.php'; And when I try to include it from the root index.php instead of the one in the views folder it will render it as require_once 'header.php'; Is this correct? Now is there a way to add this to a config file so that I don't have to make my include files so verbose? How do I do that? I found that the $_SERVER["DOCUMENT_ROOT"] option was a nuisance because I'm developing using a local server, which means I have to change all my paths when I deploy. Thanx. Quote Link to comment https://forums.phpfreaks.com/topic/262486-proper-way-to-do-includes-for-small-site/#findComment-1345191 Share on other sites More sharing options...
requinix Posted May 13, 2012 Share Posted May 13, 2012 __DIR__ is the directory containing the currently-executing file (exactly like how __FILE__ is the full path to the file). So if you use require_once __DIR__ . "/header.php"; in /forum/views/*.php then it'll include /forum/views/header.php, and if you use it in /index.php then it'll include /header.php. I found that the $_SERVER["DOCUMENT_ROOT"] option was a nuisance because I'm developing using a local server, which means I have to change all my paths when I deploy. Why's that? Are you serving the site out of a subfolder or something? Quote Link to comment https://forums.phpfreaks.com/topic/262486-proper-way-to-do-includes-for-small-site/#findComment-1345194 Share on other sites More sharing options...
philosopherdog Posted May 13, 2012 Author Share Posted May 13, 2012 cool thanx. Why's that? Are you serving the site out of a subfolder or something? Yes. Actually, when I tried that it wanted the whole path to the apache server as part of the include. I'm assuming this is going to be different when I deploy it since it's going up on a windows server (unfortunately). Quote Link to comment https://forums.phpfreaks.com/topic/262486-proper-way-to-do-includes-for-small-site/#findComment-1345198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.