THEK Posted December 22, 2011 Share Posted December 22, 2011 Hi, I'm working on a project which was going pretty well until I discovered the application doesn't actually know where it is installed. So far when I want to include the config file I just use $_SERVER['DOCUMENT_ROOT'].'/config.php'; Which works fine if the application is installed in the root directory. But how do I get the folder the application is installed in? e.g. Document Root Folder: C:\Web\ Actually Application Folder: C:\Web\MyApp\ How do I get the \MyApp dynamically? I can't just use './' all the time because I have files accessed by AJAX stored in other folders. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/253685-find-where-the-application-is-installed/ Share on other sites More sharing options...
scootstah Posted December 22, 2011 Share Posted December 22, 2011 dirname(__FILE__); Quote Link to comment https://forums.phpfreaks.com/topic/253685-find-where-the-application-is-installed/#findComment-1300522 Share on other sites More sharing options...
THEK Posted December 23, 2011 Author Share Posted December 23, 2011 dirname(__FILE__); I have a config file that contains database information and some constants. This is generally included in every page. Including one's which are accessed via an AJAX request. My problem is I've only been testing on my website with the application running from the root directory so I was including the config file from all files like so: PHP Code: require_once $_SERVER['DOCUMENT_ROOT'].'/config.php'; This has a problem, as I picked up on in the first bit of testing, if someone installs it to say: WEBROOT/myApp/ The config file will be unreachable. My question is what is the easiest way to get around this? If I didn't use ajax I wouldn't need to worry about this because I could set the require statement in the main file and all the other includes would pick up on it. However, because I do use AJAX there are files within folders that would need different levels of dirname before getting to the right one. I was thinking of storing the root directory in a hidden input and sending that as data with the AJAX but is this the best solution? Quote Link to comment https://forums.phpfreaks.com/topic/253685-find-where-the-application-is-installed/#findComment-1300966 Share on other sites More sharing options...
trq Posted December 24, 2011 Share Posted December 24, 2011 Ajax requests are no different to any other request as far as PHP is concerned. Quote Link to comment https://forums.phpfreaks.com/topic/253685-find-where-the-application-is-installed/#findComment-1301006 Share on other sites More sharing options...
kicken Posted December 24, 2011 Share Posted December 24, 2011 Just use a relative path from your file to the included file for your config include. The files' relative locations to each other shouldn't be changing. require '../../config.php'; Quote Link to comment https://forums.phpfreaks.com/topic/253685-find-where-the-application-is-installed/#findComment-1301008 Share on other sites More sharing options...
scootstah Posted December 24, 2011 Share Posted December 24, 2011 dirname(__FILE__); gives you the directory to the file. So if everything is running out of your index.php in the webroot, then all you to do is put this function in front of every include. It doesn't matter if the webroot changes, because it will still give the path to the file that called it. AJAX has no bearing on where files are. Quote Link to comment https://forums.phpfreaks.com/topic/253685-find-where-the-application-is-installed/#findComment-1301009 Share on other sites More sharing options...
THEK Posted December 24, 2011 Author Share Posted December 24, 2011 dirname(__FILE__); gives you the directory to the file. So if everything is running out of your index.php in the webroot, then all you to do is put this function in front of every include. It doesn't matter if the webroot changes, because it will still give the path to the file that called it. AJAX has no bearing on where files are. I tried that but it looks messy when I'm within several folders. dirname(dirname(dirname(dirname(__FILE__)))); Just use a relative path from your file to the included file for your config include. The files' relative locations to each other shouldn't be changing. require '../../config.php'; When I do ../../config.php; it can't find it. But if I do ././config.php it can. Is that right? **EDIT** Found the problem. This is where I was talking about ajax issues. I use the same file through both AJAX and includes. I include it when the page loads and use ajax when they want to refresh it or a change is made. If I access it through AJAX I have to use ../../config.php But when including it I have to use ././config.php Quote Link to comment https://forums.phpfreaks.com/topic/253685-find-where-the-application-is-installed/#findComment-1301129 Share on other sites More sharing options...
trq Posted December 24, 2011 Share Posted December 24, 2011 I tried that but it looks messy when I'm within several folders. dirname(dirname(dirname(dirname(__FILE__)))); That code makes no sense. dirname returns a complete path. There is no need to pass it's output back into dirname for each directory in the tree. Quote Link to comment https://forums.phpfreaks.com/topic/253685-find-where-the-application-is-installed/#findComment-1301204 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.