php get URL of current file directory

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



php get URL of current file directory



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.





Using $_SERVER['DOCUMENT_ROOT'] is a lot easier than trying to calculate a relative directory.
– aynber
Aug 10 at 15:22


$_SERVER['DOCUMENT_ROOT']





Possible duplicate of PHP - Convert File system path to URL
– gbalduzzi
Aug 10 at 15:22





In most modern web applications the URL bears no relevance to the filesystem whatsoever... just something to bear in mind.
– CD001
Aug 10 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 at 15:25





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 at 15:53





4 Answers
4



Use echo $_SERVER['PHP_SELF'];


echo $_SERVER['PHP_SELF'];



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


http://localhost/~andy/test.php



The output would be:



/~andy/test.php


/~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']);


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


/~andy


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


echo getcwd();



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





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 at 15:44





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.
– Andy
Aug 10 at 15:53



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


https://localhost.com/this/is/a/url



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


$_SERVER['DOCUMENT_ROOT']



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


$_SERVER['PHP_SELF']



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


$_SERVER['SERVER_NAME']



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



$_SERVER['HTTP_REFERER']



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



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


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


$_SERVER['PHP_SELF'];



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


$_SERVER['REQUEST_URI']



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


substr()


strrpos()



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





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 at 15:48


dirname($_SERVER['PHP_SELF']);


substr()


strpos()



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard