Nexus10 Posted July 10, 2010 Share Posted July 10, 2010 I have an include on my site. The pages it is requested from (page1) may look like mysite.com/page1 or mysite.com/business/sales (which is in-fact page1 shown by mod_rewrite) etc. How can I write the include which is on that page1 so that it goes to the top of the directory first so we can then go through to the right directory? Also, I tried going include('http://www.mysite.com/thefiletocinlude.php'); although thsi fails to work, anyone know why? Quote Link to comment https://forums.phpfreaks.com/topic/207370-how-to-get-to-top-of-file-directoy/ Share on other sites More sharing options...
Wolphie Posted July 10, 2010 Share Posted July 10, 2010 You can use $_SERVER['DOCUMENT_ROOT'] to get the path of the webservers root directory. include(dirname($_SERVER['DOCUMENT_ROOT']) . DIRECTORY_SEPARATOR . 'includefile.php'); Quote Link to comment https://forums.phpfreaks.com/topic/207370-how-to-get-to-top-of-file-directoy/#findComment-1084157 Share on other sites More sharing options...
Nexus10 Posted July 10, 2010 Author Share Posted July 10, 2010 Thanks, but that doesn't solve my problem. Quote Link to comment https://forums.phpfreaks.com/topic/207370-how-to-get-to-top-of-file-directoy/#findComment-1084166 Share on other sites More sharing options...
Nexus10 Posted July 10, 2010 Author Share Posted July 10, 2010 More info: http://www.phpfreaks.com/forums/index.php/topic,303860.0.html Quote Link to comment https://forums.phpfreaks.com/topic/207370-how-to-get-to-top-of-file-directoy/#findComment-1084173 Share on other sites More sharing options...
Zane Posted July 10, 2010 Share Posted July 10, 2010 ../business/sales Quote Link to comment https://forums.phpfreaks.com/topic/207370-how-to-get-to-top-of-file-directoy/#findComment-1084195 Share on other sites More sharing options...
Nexus10 Posted July 11, 2010 Author Share Posted July 11, 2010 ../business/sales I tried this but it only works on the exact url www.mysite.com/folder/register.php etc, not on the mod_rewrite url's www.mysite.com/folder/register/ etc. Quote Link to comment https://forums.phpfreaks.com/topic/207370-how-to-get-to-top-of-file-directoy/#findComment-1084285 Share on other sites More sharing options...
myrddinwylt Posted July 11, 2010 Share Posted July 11, 2010 This is the code I use to allow me to get by the problem as stated. /config.inc.php define('BASE_URL','http://' . $_SERVER['HTTP_HOST'] . str_replace('config.inc.php','',str_replace($_SERVER['DOCUMENT_ROOT'],'', str_replace('\\','/',__FILE__)))); define('BASE_REAL_PATH',str_replace('config.inc.php','', str_replace('\\','/',__FILE__))); In subfolders, I do the following. include('/config.inc.php'); echo BASE_REAL_PATH; I think I know why you wish to do this. Some hosts do not allow the use of "../" for includes, as well as the fact that doing a "../" include can be unreliable at times due to different folders, sub-folders, etc, and if you are using the script in SEO, then the current direction could be faked, and thus, the "../" include would fail (see phpBB for plenty of examples of this). The above code has allowed me to do a direct include without ever encountering any of the stated problems. You can name "config.inc.php" whatever you wish, just make sure to replace any instances of it in the code with the new file name. It is also strongly recommended that this is placed in the root / directory to simplify access to it from any sub-folder scripts using the second block of code. Quote Link to comment https://forums.phpfreaks.com/topic/207370-how-to-get-to-top-of-file-directoy/#findComment-1084289 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.