FalcorTheDog Posted July 20, 2009 Share Posted July 20, 2009 My server code is littered with includes/requires of the form: include('/includes/somefile.php'); It works fine on the server, since my "includes" folder is in the root directory on the web server. However, now I have a test server setup on my local machine, and am trying to test the same code without changing all of the includes, but the backslash ('/') is pointing to the root directory on my computer instead of the root folder of my local server. It looked as if editing the doc_root in my php.ini file would solve this problem, but I haven't had any luck. Any ideas? (I'm running MAMP locally on OS X 10.5) Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/166606-include-paths-and-doc_root/ Share on other sites More sharing options...
ignace Posted July 20, 2009 Share Posted July 20, 2009 Although not entirely sure: /includes/somefile.php is equal to: ../includes/somefile.php iOr Is the operating system from your server different then your local server? Quote Link to comment https://forums.phpfreaks.com/topic/166606-include-paths-and-doc_root/#findComment-878517 Share on other sites More sharing options...
Q Posted July 20, 2009 Share Posted July 20, 2009 This is why you should always use specific paths. As ignace is saying, it would work with the ../ and ./ instead of your / (Which by default refers to the root of the server) Using / makes it very difficult to move your site to another server in the future, since it may not be possible to place the site in the root folder. You should consider remaking your system to use specific paths Quote Link to comment https://forums.phpfreaks.com/topic/166606-include-paths-and-doc_root/#findComment-878524 Share on other sites More sharing options...
FalcorTheDog Posted July 20, 2009 Author Share Posted July 20, 2009 Yeah, it appears to work if I change each include to use './' but so does this mean I'm SOL and have to rework all my code? Using paths relative to the root are nice so that you can move files around in the directory structure on your server without worrying about breaking things, but now I wonder if it's worth it. Quote Link to comment https://forums.phpfreaks.com/topic/166606-include-paths-and-doc_root/#findComment-879079 Share on other sites More sharing options...
ignace Posted July 21, 2009 Share Posted July 21, 2009 but so does this mean I'm SOL and have to rework all my code? No, your code and SQL will still work you only need to adjust the paths. Using paths relative to the root are nice so that you can move files around in the directory structure on your server without worrying about breaking things, but now I wonder if it's worth it. That's not true. If you have a file located in /includes/somefile.php and now I move somefile.php to /somedir. Then how will this code still works? require_once('/includes/somefile.php'); Consider to always use absolute paths using either realpath() or by defining directory constants: defined('__DIR__') or define('__DIR__', dirname(__FILE__)); //__DIR__ exists as of PHP 5.3.0 define('DOCUMENT_ROOT', __DIR__); require_once(DOCUMENT_ROOT . '/includes/somefile.php'); Personally I prefer realpath() as it modifies the directory separator to the underlying operating systems' directory separator. Quote Link to comment https://forums.phpfreaks.com/topic/166606-include-paths-and-doc_root/#findComment-879475 Share on other sites More sharing options...
FalcorTheDog Posted July 23, 2009 Author Share Posted July 23, 2009 That's not true. If you have a file located in /includes/somefile.php and now I move somefile.php to /somedir. Then how will this code still works? You're correct. What I meant was: if I wanted to move the file that was actually doing the "including" to another directory, I could do so without changing the relative path of every include within that file. I think the best bet in the future is to have a constant defined with the root path and just concatenate it with the beginning of every include path. But I'm still really baffled that I can't just tell PHP which directory I want to function as the root... Quote Link to comment https://forums.phpfreaks.com/topic/166606-include-paths-and-doc_root/#findComment-881021 Share on other sites More sharing options...
ignace Posted July 23, 2009 Share Posted July 23, 2009 But I'm still really baffled that I can't just tell PHP which directory I want to function as the root... You can, however only in safe_mode (or by using chdir()) and if you have php.ini access as doc_root is PHP_INI_SYSTEM. However /includes/file.php will still throw errors i guess as you then are trying to access files outside the doc_root. Just remember to always start without a / (require_once('includes/file.php') which assumes the current work directory (getcwd()). Maybe user_dir can help: http://www.php.net/manual/en/ini.core.php#ini.user-dir Quote Link to comment https://forums.phpfreaks.com/topic/166606-include-paths-and-doc_root/#findComment-881059 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.