Checking relative absolute paths / urls in PHP

I need to implement functions to check if paths and URLs are relative, absolute or invalid (syntactically invalid, and if the resource does not exist). What range of cases should I look for?

function check_path($dirOrFile) { // If it an absolute path: (Anything that starts with a '/'?) return 'absolute'; // If it a relative path: return 'relative'; // If it an invalid path: return 'invalid'; } function check_url($url) { // If it an absolute url: (Anything that starts with a 'http://' or 'https://'?) return 'absolute'; // If it a relative url: return 'relative'; // If it an invalid url: return 'invalid'; } 
+8
source share
6 answers

Absolute paths and URLs

You're right, absolute URLs in Linux should start with / , so checking for a slash at the beginning of the path will suffice.

For URLs you need to check http:// and https:// , however, as you wrote, there are more URLs starting with ftp:// , sftp:// or smb:// . Thus, it very much depends on what range of use you want to cover.

Invalid paths and URLs

Assuming you mean Linux, the only characters that are forbidden in the path are / and \0 . It actually depends a lot on the file system, however you can assume that this was correct for most applications.

On Windows, this is more complicated. You can read about this in the Path.GetInvalidPathChars documentation in the Remarks section.

URLs are more complex than Linux paths, since only valid characters are AZ , AZ , 0-9 , - 0-9 , _ , ~ , : , / ? , # , [ , ] , @ ! , $ , & , ' , ( , ) , * , + ; and = (as described in another answer here ).

Relative paths and URLs

In general, paths and URLs that are not absolute or invalid are relative.

+5
source

Since I cannot comment on the answers due to my bad reputation, I have to respond to ymakux's answer with a function that he copied from the Drupal library.

I use this function and I found that the urls with the request part (text after the character?) That contains | character will be evaluated as false

eg:

 https://example.com/image.jpeg?fl=res,749,562,3|shr,,20|jpg,90 

False will be evaluated.

All you have to do is add

\ | |

To request part of a regular expression so that the function looks like this:

 public static function isAbsoluteUrl($url) { $pattern = "/^(?:ftp|https?|feed)?:?\/\/(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)* (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2}) +@ )?(?: (?:[a-z0-9\-\.]|%[0-9a-f]{2})+|(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]))(?::[0-9]+)?(?:[\/|\?] (?:[\w#!:\.\?\+\|=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})*)?$/xi"; return (bool) preg_match($pattern, $url); } 

Hope this helps someone :)

+3
source

I recently started creating a compositional package that can be useful for checking the URL of a URL against / absolute (and moreover, of course).

Check out the repository here: https://github.com/Enrise/UriHelper Or the Packagists composer here: https://packagist.org/packages/enrise/urihelper

Some examples:

 $uri = new \Enrise\Uri('http://usr: pss@example.com :81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment'); echo $uri->getScheme(); // http echo $uri->getUser(); // usr echo $uri->getPass(); // pss echo $uri->getHost(); // example.com echo $uri->getPort(); // 81 echo $uri->getPath(); // /mypath/myfile.html echo $uri->getQuery(); // a=b&b[]=2&b[]=3 echo $uri->getFragment(); // myfragment echo $uri->isSchemeless(); // false echo $uri->isRelative(); // false $uri->setScheme('scheme:child:scheme.VALIDscheme123:'); $uri->setPort(null); echo $uri->getUri(); //scheme:child:scheme.VALIDscheme123:usr: pss@example.com /mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment 
+2
source

This feature is taken from Drupal.

 public function is_absolute($url) { $pattern = "/^(?:ftp|https?|feed):\/\/(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)* (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2}) +@ )?(?: (?:[a-z0-9\-\.]|%[0-9a-f]{2})+|(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]))(?::[0-9]+)?(?:[\/|\?] (?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})*)?$/xi"; return (bool) preg_match($pattern, $url); } 
+1
source

From the Symfony FileSystem component to check if the path is absolute:

 public function isAbsolutePath($file) { return strspn($file, '/\\', 0, 1) || (strlen($file) > 3 && ctype_alpha($file[0]) && substr($file, 1, 1) === ':' && strspn($file, '/\\', 2, 1) ) || null !== parse_url($file, PHP_URL_SCHEME) ; } 
+1
source

If you already know that the URL is correctly formed :

 if(strpos($uri,'://')!==false){ //protocol: absolute url }elseif(substr($uri,0,1)!='/'){ //leading '/': absolute to domain name (half relative) }else{ //no protocol and no leading slash: relative } 
0
source

Source: https://habr.com/ru/post/897176/


All Articles