10

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

4
  • Are you sure you haven't confused rootfolder and foldername in examples? Commented Sep 3, 2012 at 6:06
  • how can You at the same time have and don't have absolute path? You should check dirname(), realpath() functions and FILE constant... Commented Sep 3, 2012 at 6:11
  • dirname() and realpath requires that the script and path should be in same server but I have on different servers Commented Sep 3, 2012 at 6:26
  • zerkms, they are the same things, I mean the foldername or dirname Commented Sep 3, 2012 at 6:28

5 Answers 5

22

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

Sign up to request clarification or add additional context in comments.

2 Comments

This does not work for user_dir paths, where /~user/... maps to something inside user's home directory. In that case you have to use $_SERVER['CONTEXT_DOCUMENT_ROOT']and remove the ~userportion from the url.
$_SERVER['REAL_DOCUMENT_ROOT'] should also be checked. You may also need to check $_SERVER['REDIRECT_URL']. It is a bit of a minefield.
12

You can use the parse_url function to get the local part of the URL.

Converting that into a file path then requires that you know where the document root of the webserver is. If it is the same server as the one the page is running on, then you can usually get that from $_SERVER['DOCUMENT_ROOT'].

It also depends on there being nothing preventing the URL from not being a direct mapping onto a file system (such as mod_rewrite, mod_alias, URIs being routed through an MVC framework, etc). For example, for a system I'm working on at the moment, if you were to hit http://example.com/blog/2012/01/01/ then the files involved would be /home/user/recall/script/recall.psgi and /home/user/recall/root/blog/day.tt but the DocumentRoot would be /home/user/recall/htdocs/)

2 Comments

my script is on different server and the path is of different
Then you need to know how that server is configured before you can calculate where on its file system a URI resolves to.
0

Try $_SERVER['DOCUMENT_ROOT']. Assuming a URL like

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

and the file's actual location on the server being

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

2 Comments

my script is on different server and the path is of different
Then you'll need to know what that doc root is. Such data is NOT revealed by standard http requests.
0

try

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

this may help you.

Comments

0

I've found this to work reliably:

function get_file_path_from_url( $file_url ){
   return realpath($_SERVER['DOCUMENT_ROOT'] . parse_url( $file_url, PHP_URL_PATH ));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.