Convert URL to file system path

2020-07-06 03:43发布

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 Linux.

Any way to do this in PHP?

标签: php
3条回答
叛逆
2楼-- · 2020-07-06 03:50
$str = "/images/test.jpg";
$str = realpath("." . $str);
查看更多
混吃等死
3楼-- · 2020-07-06 03:54

It sounds like you want the realpath function.

查看更多
一夜七次
4楼-- · 2020-07-06 04:05

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.

查看更多
登录 后发表回答