peterpostmann/fileuri

Returns a file uri from a (relative) path

1.0.0 2017-11-05 11:20 UTC

This package is not auto-updated.

Last update: 2024-11-06 09:07:59 UTC


README

Software License Build Status

Returns a file uri from a (relative) path

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.

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:


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)

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:


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.

use function peterpostmann\fileuri;

// Relative Path without base path
var_dump(fileuri('relative/path/to/file.ext'));

The above example will output:


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.

License

The MIT License (MIT). Please see License File for more information.