PHP - Convert File System Path to Url

Convert URL to file system path

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

How can I convert a fully qualified file path to URL in Laravel

How about:

str_replace(public_path(), '', $file); 

PHP: Systems path to actual URL

Here you go, I made this function and it works perfectly:

function getPath($path)
{
$url = "http".(!empty($_SERVER['HTTPS'])?"s":"").
"://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$dirs = explode('/', trim(preg_replace('/\/+/', '/', $path), '/'));
foreach ($dirs as $key => $value)
if (empty($value)) unset($dirs[$key]);
$parsedUrl = parse_url($url);
$pathUrl = explode('/', trim($parsedUrl['path'], '/'));
foreach ($pathUrl as $key => $value)
if (empty($value)) unset($pathUrl[$key]);
$count = count($pathUrl);
foreach ($dirs as $key => $dir)
if ($dir === '..')
if ($count > 0)
array_pop($pathUrl);
else
throw new Exception('Wrong Path');
else if ($dir !== '.')
if (preg_match('/^(\w|\d|\.| |_|-)+$/', $dir)) {
$pathUrl[] = $dir;
++$count;
}
else
throw new Exception('Not Allowed Char');
return $parsedUrl['scheme'].'://'.$parsedUrl['host'].'/'.implode('/', $pathUrl);
}

Example:

Let's say your current location is http://www.mywebsite.com/files/morefiles/movies,


echo getPath('../songs');

OUTPUT

http://www.mywebsite.com/files/morefiles/songs

echo getPath('./songs/whatever');

OUTPUT

http://www.mywebsite.com/files/morefiles/movies/songs/whatever

echo getPath('../../../../../../');

In this case it will throw an exception.


NOTE

I use this regex '/^(\w|\d|\.| |_|-)+$/' to check the path directories which mean that only digit, word characters, '.', '-', '_' and ' ' are allowed. An exception will be trown for others characters.

The path .///path will be corrected by this function.

USEFUL

  • explode
  • implode
  • array_pop
  • parse_url
  • trim
  • preg_match
  • preg_replace

PHP: Get absolute path from absolute URL

You can use the parse_url function to get the local part of the URL.

Converting that into a file path then requires that you know where the document root of the webserver is. If it is the same server as the one the page is running on, then you can usually get that from $_SERVER['DOCUMENT_ROOT'].

It also depends on there being nothing preventing the URL from not being a direct mapping onto a file system (such as mod_rewrite, mod_alias, URIs being routed through an MVC framework, etc). For example, for a system I'm working on at the moment, if you were to hit http://example.com/blog/2012/01/01/ then the files involved would be /home/user/recall/script/recall.psgi and /home/user/recall/root/blog/day.tt but the DocumentRoot would be /home/user/recall/htdocs/)

php get URL of current file directory

I've found a solution here:
https://stackoverflow.com/a/1240574/7295693

This is the code I'll now be useing:

function get_current_file_url($Protocol='http://') {
return $Protocol.$_SERVER['HTTP_HOST'].str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(__DIR__));
}


Related Topics



Leave a reply



Submit