This package is abandoned. Please consider using an stable alternative for any production code.

File URI

Starting with version 1.1.0 all URI objects are defined in the League\Uri namespace. The League\Uri\Schemes namespace is deprecated and will be removed in the next major release.

To ease working with File URIs, the library comes bundle with a URI specific File class.

Instantiation

In addition to the defined named constructors, because file path depends on the underlying OS, you can also instantiate a new File URI object from a file path using:

  • the createFromUnixPath named constructor
  • the createFromWindowsPath named constructor
<?php

use League\Uri;

$uri = Uri\File::createFromWindowsPath('c:\windows\My Documents\my word.docx');
echo $uri; //returns 'file://localhost/c:My%20Documents/my%20word.docx'

Validation

Even though all URI properties are defined and accessible attempt to set any component other than the scheme, the host, and the path will result in the object throwing a InvalidArgumentException exception. As adding content to theses URI parts will generate an invalid File URI.

<?php

use League\Uri;

$uri = Uri\File::createFromUnixPath('/path/./../relative');
$uri->withQuery('foo=bar'); // will throw an League\Uri\UriException

URI normalization

If the host file is the empty string it will be converted to localhost.

<?php

use League\Uri;

$uri = Uri\File::createFromString('file:///path/to/file.csv');
echo $uri; //display file://localhost/path/to/file.csv