Download the PHP package peterpostmann/fileuri without Composer
On this page you can find all versions of the php package peterpostmann/fileuri. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download peterpostmann/fileuri
More information about peterpostmann/fileuri
Files in peterpostmann/fileuri
Package fileuri
Short Description Returns a file uri from a (relative) path
License MIT
Informations about the package fileuri
fileuri
Returns a file uri from a (relative) path
Install
Via Composer
If you dont want to use composer just copy the fileuri.php file and include it into your project.
Why
This function can be used to create RFC3986 complient file URIs and build protocol agnostic functions.
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:
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)
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:
Error Output
The function returns false if a relative path is given without a base path.
use function peterpostmann\fileuri;
// Relative Path without base path
var_dump(fileuri('relative/path/to/file.ext'));
The above example will output:
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.
License
The MIT License (MIT). Please see License File for more information.