Reputation: 901
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.
Upvotes: 13
Views: 43516
Reputation: 1043
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'
Upvotes: 0
Reputation: 11
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']);
}
}
Upvotes: 0
Reputation: 901
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__));
}
Upvotes: 6
Reputation: 5404
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
Upvotes: 11
Reputation: 12508
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.phpWith 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
Upvotes: 1
Reputation: 649
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'];
Upvotes: 6