13

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.

9
  • 3
    Using $_SERVER['DOCUMENT_ROOT'] is a lot easier than trying to calculate a relative directory. Commented Aug 10, 2018 at 15:22
  • Possible duplicate of PHP - Convert File system path to URL Commented Aug 10, 2018 at 15:22
  • 1
    In most modern web applications the URL bears no relevance to the filesystem whatsoever... just something to bear in mind. Commented Aug 10, 2018 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 Commented Aug 10, 2018 at 15:25
  • 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. Commented Aug 10, 2018 at 15:53

6 Answers 6

12

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

Sign up to request clarification or add additional context in comments.

2 Comments

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
Have a look at 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.
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'];

Comments

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__)); 
}

1 Comment

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
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

1 Comment

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.
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']);
    }

}

Comments

0

Based on some of the comments above (with some editing) this code will get a link 'my_file.php' in the current directory and display it in the devtools console of a javascript program.

<?php 
$link= $_SERVER['REQUEST_SCHEME'].'://' . $_SERVER['HTTP_HOST']. substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], "/")) .'/my_file.php' ;

echo "console.log('$link')"
?>

the $_SERVER['REQUEST_SCHEME'] give you either 'http' or 'https'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.