Convert URL to file system path
Is there a way to convert a web URL in to the absolute file system path (independent from OS)?
For example: I have an 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 Linu开发者_JAVA技巧x.
Any way to do this in PHP?
$str = "/images/test.jpg";
$str = realpath("." . $str);
This will give you /images/test.jpg
:
$path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $path)
Where $_SERVER['DOCUMENT_ROOT']
gives you the document root directory under which the current script is executing.
It sounds like you want the realpath function.
精彩评论