Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php get URL of current file directory

Tags:

php

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.

  • http://localhost:8888/index.php calls: "http://localhost:8888/dir1/dir2/dir3/myfile.php"
  • "myfile.php" needs to know it's place in the universe :) "Where am I"
  • "myfile.php" should know it's url location is "http://localhost:8888/dir1/dir2/dir3/"
like image 306
Tim Avatar asked Aug 10 '18 15:08

Tim


People also ask

How can get current working directory in PHP?

Use the getcwd() Function to Get the Current Directory Name in PHP. The getcwd() function gives the current working directory. The returned value is a string on success.

What is __ DIR __ in PHP?

The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.

Where is the path of the PHP FILE?

In PHP you can access it via $_SERVER['DOCUMENT_ROOT'] . $path = $_SERVER['DOCUMENT_ROOT'] . "/forum/index.

What is dirname (__ FILE __) in PHP?

dirname(__FILE__) allows you to get an absolute path (and thus avoid an include path search) without relying on the working directory being the directory in which bootstrap. php resides. (Note: since PHP 5.3, you can use __DIR__ in place of dirname(__FILE__) .)


4 Answers

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

like image 105
Andy Avatar answered Oct 09 '22 11:10

Andy


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'];

like image 22
adamoffat Avatar answered Oct 09 '22 09:10

adamoffat


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

like image 31
War10ck Avatar answered Oct 09 '22 09:10

War10ck


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__)); 
}
like image 29
Tim Avatar answered Oct 09 '22 10:10

Tim