PHP: Get absolute path from absolute URL

Tariq Aziz picture Tariq Aziz · Sep 3, 2012 · Viewed 32k times · Source

I have an absolute path of a file, Is there a way to get the file absolute path

http://domainname/rootfolder/filename.php

and I wanna get something like

/home/domainname/public_html/foldername/filename.php

Answer

carlosveucv picture carlosveucv · Jul 19, 2013

This is the easiest way:

$path = parse_url('http://domainname/rootfolder/filename.php', PHP_URL_PATH);

//To get the dir, use: dirname($path)

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

That'll print you the full path.

Documentation:

parse_url

DOCUMENT_ROOT

dirname