PHP从URL获取绝对路径

I have this URL on localhost: http://localhost:7777/somesite/sites/default/files/devel-7.x-1.5.zip and want to get c:\xampp\htdocs\somesites\default\files\devel-7.x-1.5.zip.

As mentioned on this question PHP: Get absolute path from absolute URL:

 $path = parse_url($url, PHP_URL_PATH);
 echo $_SERVER['DOCUMENT_ROOT'] . $path;

The above snippet should let me get the actual path of the file. Unfortunately this is not working. When printing $path it returns the $url instead of somesites\default\files. Could this be because I'm running it on localhost:7777?

You can do this with server variables:

echo $_SERVER['DOCUMENT_ROOT'] . $_SERVER['REQUEST_URI'];

Try

$path = parse_url($url);
echo str_replace('/', "\\", $_SERVER['DOCUMENT_ROOT'].$path['host'].$path['path']);

This might be causing because correct url is not being passed to parse_url function. Print the $url value before passing to parse_url function and check if it is printing appropriate value. You might be passing something like this http://http://localhost:7777/http://localhost:7777/somesite/sites/default/files/devel-7.x-1.5.zip in $url because of which when parse_url processes the $url, it returns your original $url.

Hope this helps :)