Convert url to file system path

Is there a way to convert web URLs to an absolute path to the file system (regardless of OS)?

For example: I have a URL /images/test.jpg ( http://www.example.com/images/test.jpg ) and I need to get:

  • `c: \ path \ to \ webroot \ images \ test.jpg`` on Windows,
  • /var/path/to/webroot/images/test.jpg on Linux.

How to do it in PHP?

+7
source share
3 answers
 $str = "/images/test.jpg"; $str = realpath("." . $str); 
+5
source

This will give you /images/test.jpg :

 $path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $path) 

Where $_SERVER['DOCUMENT_ROOT'] provides the root directory of the document in which the current script is running.

+3
source

Looks like you want a realpath .

-one
source

All Articles