Jump to content

Finding URL Path


Larry101

Recommended Posts

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

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.