Return current web path in PHP
Solution 1
There are four existing answers, but they all seem to deal with file paths, and you're asking about a base URL for web requests.
Given any web request, you get a bunch of keys in $_SERVER
that may be helpful. For example, in your mock example, you might have the following:
-
http://cms.dev/folder/ —
$_SERVER['REQUEST_URI'] == /folder/
-
http://cms.dev/folder/index.php —
$_SERVER['REQUEST_URI'] == /folder/index.php
-
http://cms.dev/folder/index.php/some/pathinfo —
$_SERVER['REQUEST_URI'] == /folder/index.php/some/pathinfo
-
http://cms.dev/folder/some/modrewrite —
$_SERVER['REQUEST_URI'] == /folder/some/modrewrite
Thinking critically, how would you pull out the base URL for any given subrequest? In certain cases you can look at $_SERVER['REQUEST_URI']
and strip off trailing elements if you know how deep in your hierarchy the request is. (For example, if your script is two folders deep, strip off the last two path elements.) When PATH_INFO
or mod_rewrite
are in use, things become less clear: as longer and longer URLs are provided, there is no clear indication where the paths end and the dynamic URL begins.
This is why WordPress, MediaWiki, phpBB, phpMyAdmin, and every application I've ever written has the user manually specify a base URL as part of the application configuration.
Solution 2
__FILE__
is a magic constant that returns the entire path of the current script. Combine with dirname and add ".." appropriately. It's more reliable than getcwd
, since it cannot change during execution.
You can then strip off the web root, to get the relative path to your script (should map to URL). There are many $_SERVER
variables that have this information. I suggest using the file system to determine:
- If your script is publicly accessible?
- At which depth / URL prefix?
Then combine with your base URL. If your script's path ==
/home/public/foo_1/script.php
... and your $_SERVER['DOCUMENT_ROOT'] ==
/home/public
Then you can rewrite your URL as /foo_1/script.php
. You don't need the fully qualified URL, unless you want it. This technique works best if you execute it from a central location, like an autoloader.
Solution 3
In order to make urls work check the base tag:
<base href="http://cms.dev/folder/" />
If the PHP file paths are the issue go with Pestilance's advice:
dirname(__FILE__) // returns the directory of current file
Solution 4
Theres a bunch of useful stuff available for things like this in the $_SERVER
array. Do a var_dump($_SERVER);
to see which element(s) of the array you need.
Solution 5
dirname(__FILE__);
basename(__FILE__);
print_r($_SERVER);
pathinfo('www/htdocs/index.html');
realpath('../../dir1/');
parse_url('http://username:password@hostname/path?arg=value#anchor');
BenTheDesigner
Updated on June 04, 2022Comments
-
BenTheDesigner over 1 year
Currently developing a PHP framework and have ran into my first problem. I need to be able to drop the framework into any folder on a server, no matter how many folders deep, and need to find that directory to use as a base URL.
For example, it currently works if I put the framework in the root of the server (http://cms.dev/), but if I were to put it in http://cms.dev/folder/ it does not work.
-
Jonathan Czitkovics over 13 yearsI don't think that he wants the file name in the path tho.
-
pestilence669 over 13 yearsThat's what
dirname()
does... it strips off the file name and returns the "directory" name. -
BenTheDesigner over 13 yearsThis is what I'm currently doing to define the root include path for files, but I need to define a base URL, that may not always be the web root. Any ideas?
-
BenTheDesigner over 13 yearsThis is why WordPress, MediaWiki, phpBB, phpMyAdmin, and every application I've ever written has the user manually specify a base URL as part of the application configuration. << This was the thought I had a second ago. Much appreciated.
-
Cory Kendall about 11 yearsTo format this correctly, try putting 4 spaces before every code line. After the 4 spaces, all special characters and spacing will be preserved.
-
László Papp over 9 yearsSome explanation would be nice, although the code is nearly self-sufficient, thanks.
-
László Papp over 9 yearsIt is better to always give some explanation.
-
László Papp over 9 yearsIt sounds like it should have been a comment.