How do I get the root of a folder from a url?
How to get root folder from php file?
for example
URL: http: // localhost / project_name /
Result: return "project_name"
+3
source to share
4 answers
Use parse_url () function for this
$url = $_SERVER['REQUEST_URI']);
$urlParse = parse_url($url);
echo $urlParse['hostname'];
However, this will only work if you are using a web server for this type of url
If you want this to work on localhost add some lines
$url = $_SERVER['REQUEST_URI']);
$urlParse = parse_url($url);
$path = explode('/',$urlParse ['path']);
echo $path[1]; //gives project_name in your case
+1
source to share
you can use dirname () in php
<?php
echo "1) " . dirname("/etc/passwd") . PHP_EOL; // 1) /etc
echo "2) " . dirname("/etc/") . PHP_EOL; // 2) / (or \ on Windows)
echo "3) " . dirname("."); // 3) .
?>
0
user671253
source
to share