2017 © Pedro Peláez
 

library fileuri

Returns a file uri from a (relative) path

image

peterpostmann/fileuri

Returns a file uri from a (relative) path

  • Sunday, November 5, 2017
  • by peterpostmann
  • Repository
  • 1 Watchers
  • 0 Stars
  • 178 Installations
  • PHP
  • 5 Dependents
  • 2 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 1 % Grown

The README.md

fileuri

![Software License][ico-license] Build Status, (*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)

The Versions

05/11 2017

dev-master

9999999-dev

Returns a file uri from a (relative) path

  Sources   Download

MIT

The Development Requires

by Peter Postmann

05/11 2017

1.0.0

1.0.0.0

Returns a file uri from a (relative) path

  Sources   Download

MIT

The Development Requires

by Peter Postmann

29/10 2017

2.0.1

2.0.1.0

Returns a file uri from a (relative) path

  Sources   Download

MIT

The Development Requires

by Peter Postmann

29/10 2017

2.0.0

2.0.0.0

Returns a file uri from a (relative) path

  Sources   Download

MIT

The Development Requires

by Peter Postmann

28/10 2017

1.1.0

1.1.0.0

Returns a file uri from a (relative) path

  Sources   Download

MIT

The Development Requires

by Peter Postmann

26/10 2017

1.0.1

1.0.1.0

Returns a file uri from a (relative) path

  Sources   Download

MIT

The Development Requires

by Peter Postmann