Look up in the mimemap table for the MIME types based on the extensions of
the given filenames.
guess_type(
file,
unknown = "application/octet-stream",
empty = "text/plain",
mime_extra = mimeextra,
subtype = ""
)a character vector of filenames, or filename extensions
the MIME type to return when the file extension was not found in the table
the MIME type for files that do not have extensions
a named character vector of the form c(extension = type)
providing extra MIME types (by default, mime:::mimeextra); note this MIME
table takes precedence over the standard table mimemap
a character vector of MIME subtypes, which should be of the
same length as file if provided (use an empty character string for a file
if we do not want a subtype for it)
library(mime)
# well-known file types
guess_type(c("a/b/c.html", "d.pdf", "e.odt", "foo.docx", "tex"))
#> [1] "text/html"
#> [2] "application/pdf"
#> [3] "application/vnd.oasis.opendocument.text"
#> [4] "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
#> [5] "text/plain"
# not in the standard table, but in mimeextra
guess_type(c("a.md", "b.R"), mime_extra = NULL)
#> [1] "text/markdown" "application/octet-stream"
guess_type(c("a.md", "b.R"))
#> [1] "text/markdown" "text/plain"
# override the standard MIME table (tex is text/x-tex by default)
guess_type("tex", mime_extra = c(tex = "text/plain"))
#> [1] "text/plain"
# unknown extension 'zzz'
guess_type("foo.zzz")
#> [1] "application/octet-stream"
# force unknown types to be plain text
guess_type("foo.zzz", unknown = "text/plain")
#> [1] "text/plain"
# empty file extension
guess_type("Makefile")
#> [1] "text/plain"
# we know it is a plain text file
guess_type("Makefile", empty = "text/plain")
#> [1] "text/plain"
# subtypes
guess_type(c("abc.html", "def.htm"), subtype = c("charset=UTF-8", ""))
#> [1] "text/html; charset=UTF-8" "text/html"