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
How to Decrypt a Password Hash in PHP
Why I Can Not Login to Magento Backend Using Google Chrome
PHP Variable Interpolation VS Concatenation
Error 403 in Loading Resources Like CSS and Js in My Index.Php
Finding Common Prefix of Array of Strings
Logging in to Joomla 1.5 Using External Form (Not Within Joomla Folder, But on Same Server)
Converting HTML to PDF (Not PDF to HTML) Using PHP
Access a Global Variable in a PHP Function
How to Make Strings "Xml Safe"
Simple HTML Dom File_Get_HTML Not Working - Is There Any Workaround
Fatal Error: Call to Undefined Function Imap_Open() in PHP
Cannot Use String Offset as an Array in PHP
Codeigniter Csrf - How Does It Work
PHP Regular Expression to Match Lines Starting with a Special Character
Simplexml How to Prepend a Child in a Node