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



http://www.mydomain.com

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


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


source


what you need? "\ localhost" or "c: \". in this case, your document root is localhost. and you can get it $_SERVER["DOCUMENT_ROOT"]

;

0


source


In your index.php ( __FILE__

points to the current file)

 $dir = dirname(__FILE__)

      

-1


source







All Articles