Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert URL to file system path

Tags:

php

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?

like image 694
salgua Avatar asked Mar 11 '11 21:03

salgua


2 Answers

$str = "/images/test.jpg";
$str = realpath("." . $str);
like image 66
Tim Cooper Avatar answered Oct 14 '22 03:10

Tim Cooper


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.

like image 43
yogsma Avatar answered Oct 14 '22 04:10

yogsma