11

Might be an easy question for you, but I'm breaking my head over this one.

I have a php file that needs to know it's current directory url to be able to link to something relative to itself.

For example, currently I know to get the current directory path instead of the url. When I use this I get the path:

realpath(__DIR__)

result:

/Applications/MAMP/htdocs/mysite/dir1/dir2/dir3

But this would be my desired result:

http://localhost:8888/dir1/dir2/dir3

Note that this is not the location of the current page. The page calls a file from "http://localhost:8888/dir1/dir2/dir3/myfile.php" And "myfile.php" has the script from above.

-- edit to elaborate more details -- Thanks for your answers. But I get that I need to add more detail.

Tim
  • 861
  • 1
  • 12
  • 27
  • 3
    Using `$_SERVER['DOCUMENT_ROOT']` is a lot easier than trying to calculate a relative directory. – aynber Aug 10 '18 at 15:22
  • Possible duplicate of [PHP - Convert File system path to URL](https://stackoverflow.com/questions/1240462/php-convert-file-system-path-to-url) – gbalduzzi Aug 10 '18 at 15:22
  • 1
    In most modern web applications the URL bears no relevance to the filesystem whatsoever... just something to bear in mind. – CD001 Aug 10 '18 at 15:23
  • @CD001 yes but in that case it would be impossibile to answer without more information. His desired output is actually matching the file path relative to the project directory – gbalduzzi Aug 10 '18 at 15:25
  • @gbalduzzi - that's why it was just a comment, not an answer; I suspect the application he's writing might be a little old-skool - there *might* be a better way of approaching the entire thing from the offset making the question itself redundant. – CD001 Aug 10 '18 at 15:29
  • Thanks, most of the results return the url of the file/path that is calling "myfile.php" from. I don't need the current (render) page url. But when calling "myfile.php" it needs to know inside "myfile.php" it's current directory url. I don't want to hardcode it, because, what's the use of code. – Tim Aug 10 '18 at 15:41
  • @Tim I've posted an answer which does what you want. – Andy Aug 10 '18 at 15:42
  • 1
    There's some ambigutiy in what you're asking because your question mentions "current directory url". The "current directory" and the URL can be totally different - there isn't necessarily a direct relationship between the filesystem directory of the script and its URL. – Andy Aug 10 '18 at 15:53
  • @Andy Yeah! I've found out :) What seems logic to me isn't something that's logic to someone else. ESPECIALLY A PROGRAMMER :) Makes me think of the guy/father/programmer that makes his children write a command to eat a peanut butter sandwich. – Tim Aug 10 '18 at 16:01

5 Answers5

11

Use echo $_SERVER['PHP_SELF'];

For example if the URL is http://localhost/~andy/test.php

The output would be:

/~andy/test.php

That's enough to generate a relative URL.

If you want the directory your current script is running in - without the filename - use:

echo dirname($_SERVER['PHP_SELF']);

In the case above that will give you /~andy (without test.php at the end). See http://php.net/manual/en/function.dirname.php

Please note that echo getcwd(); is not what you want, based on your question. That gives you the location on the filesystem/server (not the URL) that your script is running from. The directory the script is located in on the servers filesystem, and the URL, are 2 completely different things.

There is also a function to parse URL's built in to PHP: http://php.net/manual/en/function.parse-url.php

Andy
  • 5,142
  • 11
  • 58
  • 131
  • Thanks, but in my case it returns a path that calls "myfile.php" in the first place.'/index.php '. The parent's location in this case – Tim Aug 10 '18 at 15:44
  • Have a look at http://php.net/manual/en/function.parse-url.php. There's some ambigutiy in what you're asking because your question mentions "current directory url". The "current directory" and the "URL" can be totally different - there isn't necessarily a direct relationship between the filesystem directory of the script and its URL. It sounds to be like you're trying to work out the directory structure of the URL, in which case the solution I've given will work. – Andy Aug 10 '18 at 15:53
6

If your URL is like this: https://localhost.com/this/is/a/url

$_SERVER['DOCUMENT_ROOT'] - gives system path [/var/www/html/this/is/a/url]

$_SERVER['PHP_SELF'] - gives the route of the current file (after the domain name) [/this/is/a/url]

$_SERVER['SERVER_NAME'] - gives the domain name [localhost.com]

$_SERVER['HTTP_REFERER'] - gives the correct HTTP(S) protocol and domain name. [https://localhost.com]

If you would like to get the full url, you can do something like:

echo $_SERVER['HTTP_REFERER'] . $_SERVER['PHP_SELF'];

However, I do believe in this case, that all you need is the relative path.. and in that case you should only need to use $_SERVER['PHP_SELF'];

adamoffat
  • 639
  • 6
  • 22
6

I've found a solution here: https://stackoverflow.com/a/1240574/7295693

This is the code I'll now be useing:

function get_current_file_url($Protocol='http://') {
   return $Protocol.$_SERVER['HTTP_HOST'].str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(__DIR__)); 
}
Tim
  • 861
  • 1
  • 12
  • 27
  • I got burned on this today! Be sure to use real path for the document root too `realpath($_SERVER['DOCUMENT_ROOT'])` so symbolic links are properly resolved – SWHarden Jan 28 '21 at 22:43
1

Based on your question, I believe this will get you what your want:

$_SERVER['HTTP_HOST'] . substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], "/"));

Reference:

  • $_SERVER['HTTP_HOST'] - In your case this would return: http://localhost:8888
  • $_SERVER['REQUEST_URI'] - In your case this would return: /dir1/dir2/dir3/myfile.php

With the added substr() and strrpos() methods, you can strip the _myfile.php` off of the end to get the desired result:

http://localhost:8888/dir1/dir2/dir3

War10ck
  • 12,387
  • 7
  • 41
  • 54
  • 1
    You can use `dirname($_SERVER['PHP_SELF']);` as opposed to `substr()` or `strpos()` to do the last part. Manipulating strings may cause undersired results if the file is renamed. The above function will simply remove the script name from the directory it's in. – Andy Aug 10 '18 at 15:48
0

You can use this code for locate internal project directory

function baseURL(){
    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
        $url = "https://"; 
    }else{
        $url = "http://"; 
    }
    // Fix 
    if(dirname($_SERVER['PHP_SELF']) == "/" || dirname($_SERVER['PHP_SELF']) == "\\") {
        return $url . $_SERVER['HTTP_HOST'];
    } else {
        return $url . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
    }

}
SDanielCH
  • 1
  • 1