0

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?

Community
  • 1
  • 1
Beakal
  • 134
  • 1
  • 4
  • 15

3 Answers3

0

You can do this with server variables:

echo $_SERVER['DOCUMENT_ROOT'] . $_SERVER['REQUEST_URI'];
schellingerht
  • 5,726
  • 2
  • 28
  • 56
0

Try

$path = parse_url($url);
echo str_replace('/', "\\", $_SERVER['DOCUMENT_ROOT'].$path['host'].$path['path']);
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
0

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 :)

Domain
  • 11,562
  • 3
  • 23
  • 44