In programming, afile uniform resource identifier (URI) scheme is a specific format ofURI, used to specifically identify a file on a host computer. While URIs can be used to identify anything, there is specific syntax associated with identifying files.[1][2]
A file URI has the format
file://host/path
wherehost is thefully qualified domain name of the system on which thepath is accessible, andpath is a hierarchical directory path of the formdirectory/directory/.../name. Ifhost is omitted, it is taken to be "localhost", the machine from which the URL is being interpreted. Note that when omitting host, the slash is not omitted (while "file:///piro.txt" is valid, "file://simpen.txt" is not, although some interpreters manage to handle the latter).
RFC 3986 includes additional information about the treatment of ".." and "." segments in URIs.
file:/path (no hostname),file:///path (empty hostname), orfile://hostname/path.file://path (i.e. two slashes, without a hostname) is never correct, but is often used.There are two ways that WindowsUNC filenames (such as\\server\folder\data.xml) can be represented. These are both described in RFC 8089, Appendix E as "non-standard". The first way (called here the 2-slash format) is to represent the server name using theAuthority part of the URI, which then becomesfile://server/folder/data.xml. The second way (called here the 4-slash format) is to represent the server name as part of thePath component, so the URI becomesfile:////server/folder/data.xml. Both forms are actively used. Microsoft .NET (for example, the methodnew Uri(path)) generally uses the 2-slash form; Java (for example, the methodnew URI(path)) generally uses the 4-slash form. Either form allows the most common operations on URIs (resolving relative URIs, and dereferencing to obtain a connection to the remote file) to be used successfully. However, because these URIs are non-standard, some less common operations fail: an example is thenormalize operation (defined in RFC 3986 and implemented in the Javajava.net.URI.normalize() method) which reducesfile:////server/folder/data.xml to the unusable formfile:/server/folder/data.xml.[6]
Here are twoUnix examples pointing to the same/etc/fstab file:
file://localhost/etc/fstabfile:///etc/fstab
TheKDE environment uses URIs without an authority field:
file:/etc/fstab
Here are some examples which may be accepted by some applications on Windows systems, referring to the same, local filec:\WINDOWS\clock.avi
file://localhost/c:/WINDOWS/clock.avifile:///c:/WINDOWS/clock.avi
Here is the URI as understood by the Windows Shell API:[7]
file:///c:/WINDOWS/clock.avi
Note that the drive letter followed by a colon and slash is part of the acceptable file URI.
On Microsoft Windows systems, the normal colon (:) after a device letter has sometimes been replaced by a vertical bar (|) in file URLs. This reflected the original URL syntax, which made the colon a reserved character in a path part.
SinceInternet Explorer 4, file URIs have been standardized on Windows, and should follow the following scheme. This applies to all applications which use URLMON or SHLWAPI for parsing, fetching or binding to URIs. To convert a path to a URL, useUrlCreateFromPath, and to convert a URL to a path, usePathCreateFromUrl.[7]
To access a file "the file.txt", the following might be used.
For a network location:
file://hostname/path/to/the%20file.txt
Or for a local file, the hostname is omitted, but the slash is not (note the third slash):
file:///c:/path/to/the%20file.txt
This is not the same as providing the string "localhost" or the dot "." in place of the hostname. The string "localhost" will attempt to access the file asUNC path\\localhost\c:\path\to\the file.txt, which will not work since the colon is not allowed in a share name. The dot "." results in the string being passed as\\.\c:\path\to\the file.txt, which will work for local files, but not shares on the local system. For examplefile://./sharename/path/to/the%20file.txt will not work, because it will result insharename being interpreted as part of the DOSDEVICES namespace, not as a network share.
The following outline roughly describes the requirements.
Use the provided functions if possible. If you must create a URL programmatically and cannot access SHLWAPI.dll (for example from script, or another programming environment where the equivalent functions are not available) the above outline will help.
To aid the installed base of legacy applications on Win32PathCreateFromUrl recognizes certain URLs which do not meet these criteria, and treats them uniformly. These are called "legacy" file URLs as opposed to "healthy" file URLs.[8]
In the past, a variety of other applications have used other systems. Some added an additional two slashes. For example, UNC path\\remotehost\share\dir\file.txt would becomefile:////remotehost/share/dir/file.txt instead of the "healthy"file://remotehost/share/dir/file.txt.
File URLs are rarely used inWeb pages on the public Internet, since they are only useful if it is known that a specific file exists on the designated host or the local computer. Additionally, web browsers generally disable File URLs in web pages that were not themselves loaded from a File URL for security reasons.[9]
Thehost specifier can be used to retrieve a file from an external source. However, no specific file-retrieval protocol is specified and the interpretation of the host specifier is not well standardized, so it is only useful in specific circumstances.
If a web page wants to access files stored on the computer the web browser is running on, a modern alternative to File URLs is theHTML5 File API.