PHP: get absolute path from absolute url

I have an absolute file path, is there any way to get the absolute file path

http://domainname/rootfolder/filename.php

and I want to get something like

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

+5
source share
4 answers

You can use the functionparse_url to get the local part of the url.

Converting this to a file path requires you to know where the document root is located on the web server. If this is the same server as the one the page is running on, you can usually get this from $_SERVER['DOCUMENT_ROOT'].

, URL- (, mod_rewrite, mod_alias, URI, MVC- ..). , , , http://example.com/blog/2012/01/01/, /home/user// script/recall.psgi /home/user/recall/root/blog/day.tt, DocumentRoot /home/user/remember/htdocs/)

+9

:

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

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

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

.

:

parse_url

DOCUMENT_ROOT

dirname

+14

$_SERVER['DOCUMENT_ROOT']. URL,

http://example.com/subfolder/somefile.html

/home/sites/example.com/html/subfolder/somefile.html
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--- $_SERVER['DOCUMENT_ROOT']
0

$filepath = $_SERVER['DOCUMENT_ROOT'].'/rootfolder/filename.php'

.

0

All Articles