cyimking Posted March 13, 2012 Share Posted March 13, 2012 I been researching on how to properly include a file (config.php file), and i was wondering which is better? define(DOC_ROOT,dirname(__FILE__)); include(DOC_ROOT.'/config.php'); or... include_once "config.php"; Quote Link to comment https://forums.phpfreaks.com/topic/258866-which-is-better/ Share on other sites More sharing options...
trq Posted March 14, 2012 Share Posted March 14, 2012 They both do exactly the same thing in the end. Quote Link to comment https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327085 Share on other sites More sharing options...
requinix Posted March 14, 2012 Share Posted March 14, 2012 Almost. The first is an absolute location and will always work. The second is relative and will only work if the current working directory is the same directory running that code (and containing config.php). Quote Link to comment https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327100 Share on other sites More sharing options...
creata.physics Posted March 14, 2012 Share Posted March 14, 2012 The first code is better as the full path to the document is included, so it'll include /home/user/public_html/config.php instead of just config.php in the same directory as the code requiring it is in. Quote Link to comment https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327125 Share on other sites More sharing options...
requinix Posted March 14, 2012 Share Posted March 14, 2012 I've made it my personal mission to point out this issue: instead of just config.php in the same directory as the code requiring it is in. It's not relative to the file that's doing the requiring. It's relative to the current working directory. Generally it is the same directory as the first file that was executed, but not necessarily. Generally it doesn't change, but not necessarily. Quote Link to comment https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327132 Share on other sites More sharing options...
creata.physics Posted March 14, 2012 Share Posted March 14, 2012 I don't know if it's just me being illiterate, but this part: Generally it doesn't change, but not necessarily. confuses me. I'm now just confused about the points trying to be made. I'm saying the first part of the code is better, because config.php is in the root directory of his script. So if he did include "config.php"; inside a file in, /root/folder/, and config.php is in /root/ it would fail. if the root path passed onto /root/folder/file.php and it was included within that file, it would work. You are saying if he did include "config.php" in any file in any directory, it would try to include config.php from whatever directory you were in( the current working directory ). So if you're in /root/ and have an include there it'll check /root/, as to where if you were in /root/folder/ it would search there for the file. So I don't see how it's not relative to the files path with the file that is doing the include. If index.php is in /root/ and has include "config.php" it will search /root/ for that file, which would mean it is relative. Anyway, I'm not trying to start an argument or be a douche, I am just obviously confused by what you meant. Quote Link to comment https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327134 Share on other sites More sharing options...
requinix Posted March 14, 2012 Share Posted March 14, 2012 Huh. If you could see me now I'd have egg on my face It seems they've changed the behavior of include() and its friends to what everybody's expected they did. Now it does search the current file's directory at some point in the process and after it tries the CWD. In my defense I know I'm correct... but apparently for previous versions of PHP before 5.3.6 (what I have on-hand to test with). And going through the user comments, for starters, reaffirms that it's changed in the relatively recent past. In other words, DISREGARD THAT I SUCK EGGS. But it's still best to use absolute paths. That hasn't changed. Quote Link to comment https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327142 Share on other sites More sharing options...
trq Posted March 14, 2012 Share Posted March 14, 2012 It seems they've changed the behavior of include() and its friends to what everybody's expected they did. Now it does search the current file's directory at some point in the process and after it tries the CWD It hasn't changed. Generally, it's a good idea to have . on your include path. Quote Link to comment https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327146 Share on other sites More sharing options...
requinix Posted March 14, 2012 Share Posted March 14, 2012 It seems they've changed the behavior of include() and its friends to what everybody's expected they did. Now it does search the current file's directory at some point in the process and after it tries the CWD It hasn't changed. Generally, it's a good idea to have . on your include path. Not that. That's still relative to the CWD (and I tested this). The fact that it eventually searches the current file's directory regardless of the include_path is new. Quote Link to comment https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327354 Share on other sites More sharing options...
kicken Posted March 14, 2012 Share Posted March 14, 2012 I don't know if it's just me being illiterate, but this part: Generally it doesn't change, but not necessarily. confuses me. The point being made is that the current working directory is not necessarily the same as the directory that contains the file being run. It can be changed either through the use of chdir or by how you run your script. For instance in PHP-CLI, lets assume the following layout: /root/myscripts/script.php /root/myscripts/config.php And your currently sitting in the /root directory. When you run php myscripts/script.php then the current working directory is /root, not /root/myscripts. As such, a relative path of "config.php" would be incorrect and cause a file not found error, where as using __FILE__ to generate an absolute path would work. As has been noted though, PHP's include apparently will take this into account on it's own and locates a file using a process like: Does the given path exist? Yes, Include it Can we find it by searching include_path? Yes, include it Does it exist in getcwd()? Yes, include it Does it exist in __DIR__? Yes, include it Else, fail. That is specific to include/require though. If you were to do something like fopen or file_get_contents it would fail with a file does not exist error. Quote Link to comment https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327378 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.