salgua
salgua

Reputation: 698

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:

Any way to do this in PHP?

Upvotes: 10

Views: 15703

Answers (3)

yogsma
yogsma

Reputation: 10586

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.

Upvotes: 3

Andrew
Andrew

Reputation: 14447

It sounds like you want the realpath function.

Upvotes: -1

user142162
user142162

Reputation:

$str = "/images/test.jpg";
$str = realpath("." . $str);

Upvotes: 9

Related Questions