fileuri
![Software License][ico-license]
, (*1)
Returns a file uri from a (relative) path, (*2)
Install
Via Composer
composer require peterpostmann/fileuri
If you dont want to use composer just copy the fileuri.php
file and include it into your project., (*3)
Why
This function can be used to create [RFC3986][1] complient file URIs and build protocol agnostic functions., (*4)
function parseReference($ref)
{
list($prefix, $path) = explode('://', $ref, 2);
return [$prefix, $path];
}
var_dump(parseReference('https://server.tld/path/ressource.ext'));
var_dump(parseReference('file:///path/to/file.ext'));
The above example will output:, (*5)
array (size=2)
0 => string 'https' (length=5)
1 => string 'server.tld/path/ressource.ext' (length=29)
array (size=2)
0 => string 'file' (length=4)
1 => string '/path/to/file.ext (length=17)
If you want to examine URIs of multiple protocols this cannot be done easily because PHP does not return rfc3986 compliant URIs for files. PHP returns different formats depending on the file location and platform (http://php.net/manual/en/wrappers.file.php), (*6)
use Sabre\Uri;
function parseReference($uri)
{
return Uri\parse($uri);
}
Usage
use function peterpostmann\fileuri;
string fileuri ( string path [, string basePath] )
Example
Sample Output
use function peterpostmann\fileuri;
// Absolute Path
echo fileuri('/path/to/file.ext');
echo fileuri('C:\path\to\winfile.ext');
echo fileuri('\\\\smbserver\share\path\to\winfile.ext');
// Relative Path with base path
echo fileuri('relative/path/to/file.ext', '/');
echo fileuri('fileInCwd.ext','C:\testfolder);
// Path that is already a URI
echo fileuri('file:///path/to/file.ext');
The above example will output:, (*7)
file:///path/to/file.ext
file:///C:/path/to/winfile.ext
file:///C:/path/to/winfile.ext
file://smbserver/share/path/to/winfile.ext
file:///relative/path/to/file.ext
file:///C:/testfolder/fileInCwd.ext
file:///path/to/file.ext
Error Output
The function returns false if a relative path is given without a base path., (*8)
use function peterpostmann\fileuri;
// Relative Path without base path
var_dump(fileuri('relative/path/to/file.ext'));
The above example will output:, (*9)
boolean false
Usage with file functions
use function peterpostmann\fileuri;
// Absolute Path
$uri = fileuri('/path/to/file.ext');
var_dump(file_get_contents(urldecode($uri)));
file_get_contents
does not normalize urls, therefore file URIs cannot be used directly., (*10)
License
The MIT License (MIT). Please see License File for more information., (*11)