Larry101 Posted May 14, 2011 Share Posted May 14, 2011 I am trying to find the URL to a directory TWO levels above the script level (if you know what I mean). For example.. the script sits at http://www.domain.com/TestArea2/members/index.php BUT I need to find the path to http://www.domain.com/TestArea2/ the nearest I get is... $domain = $_SERVER['HTTP_HOST']; $domain .= $_SERVER['REQUEST_URI']; echo $domain; // outputs www.domain.com/TestArea2/members/index.php Is there a simple method to do this or have I got to split the string and work backwards? Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/236416-finding-url-path/ Share on other sites More sharing options...
Zurev Posted May 14, 2011 Share Posted May 14, 2011 Try: echo realpath("../../"); If that doesn't work, take a good look at the php functions: getcwd(); dirname(); basename(); realpath(); Quote Link to comment https://forums.phpfreaks.com/topic/236416-finding-url-path/#findComment-1215481 Share on other sites More sharing options...
Larry101 Posted May 14, 2011 Author Share Posted May 14, 2011 Try: echo realpath("../../"); If that doesn't work, take a good look at the php functions: getcwd(); dirname(); basename(); realpath(); Thanks but the realpath returned the absolute path but not the URL. Is there an equivalent to return the URL? Quote Link to comment https://forums.phpfreaks.com/topic/236416-finding-url-path/#findComment-1215485 Share on other sites More sharing options...
anupamsaha Posted May 14, 2011 Share Posted May 14, 2011 See parse_url() and reconstruct the URL in whatever way you want. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/236416-finding-url-path/#findComment-1215491 Share on other sites More sharing options...
Larry101 Posted May 14, 2011 Author Share Posted May 14, 2011 Tried all sorts but eventually wrote this... bit long winded but seems to work $domain .= $_SERVER['REQUEST_URI'];// $domain = /TestArea2/members/index.php $domain_arr = explode('/', $domain); $arr_len = count($domain_arr);//get length of array for ($i=1;$i<$arr_len-2;$i++){ //loop through except last 2 $url_path .= "/" .$domain_arr[$i]; } echo $_SERVER['HTTP_HOST'] .$url_path ."<br>"; //prints www.domain.com/TestArea2 Quote Link to comment https://forums.phpfreaks.com/topic/236416-finding-url-path/#findComment-1215499 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.