如何从URL获取文件夹根目录?

How do you get the root folder from a php file?

For example

URL: http://localhost/project_name/

Result: return "project_name"

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) .
?>

Use the 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 webserver, for these type of url

http://www.mydomain.com

If you want this to work on a localhost, add some few lines

$url = $_SERVER['REQUEST_URI']);
$urlParse = parse_url($url);

$path = explode('/',$urlParse ['path']);
echo $path[1]; //gives project_name in your case

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

In your index.php (__FILE__ points to current file)

 $dir = dirname(__FILE__)