To ease working with File URIs, the library comes bundle with a URI specific File class.
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:
<?php use League\Uri\Schemes\File; $uri = File::createFromWidowsPath('c:\windows\My Documents\my word.docx'); echo $uri; //returns 'file://localhost/c:My%20Documents/my%20word.docx'
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\Schemes\File; $uri = File::createFromUnixPath('/path/./../relative'); $uri->withQuery('foo=bar'); // will throw an League\Uri\Schemes\UriException
If the host file is the empty string it will be converted to
<?php use League\Uri\Schemes\File; $uri = File::createFromString('file:///path/to/file.csv'); echo $uri; //display file://localhost/path/to/file.csv