NaviServer - programmable web server
4.99  5.0

[ Main Table Of Contents | Table Of Contents | Keyword Index ]

ns_guesstype(n) 4.99.30 naviserver "NaviServer Built-in Commands"

Name

ns_guesstype - Lookup MIME type based on file extension

Table Of Contents

Synopsis

Description

This command guesses and returns the MIME type of a file, based on the extension of filename. A large set of default extension to MIME type mappings is built into the command:

COMMANDS

ns_guesstype filename

Return the MIME type for the provided filename based on its file extension.

The MIME type returned for a file extension can be overridden in the NaviServer configuration file, and new mappings can be added, e.g.

 ns_section "ns/mimetypes" {
   ns_param  ".xls"       "application/vnd.ms-excel"     ;# Add a mapping for Excel files
   ns_param  ".sjis_html" "text/html; charset=shift_jis" ;# Adding a mapping, specifying charset
   ns_param  ".adp"       "text/html; charset=UTF-8"     ;# Overriding an existing mapping
 }

The MIME type to be returned for files with unrecognized or missing extensions can also be specified in the "ns/mimetypes" section of the configuration file, e.g.

 ns_section "ns/mimetypes" {
   ns_param  default      "*/*"   ;# MIME type for unrecognized extension.
   ns_param  noextension  "*/*"   ;# MIME type for missing extension.
 }

If default is not configured, "*/*" will be returned for files with unrecognized extensions. If noextension is not configured, the MIME type configured as default (or "*/*") will be used.

EXAMPLES

(Assuming configured with the examples above)

 % ns_guesstype "hello.jpg"
 image/jpeg
 
 % ns_guesstype "hello.xls"
 application/vnd.ms-excel
 
 % ns_guesstype "hello.html"
 text/html
 
 % ns_guesstype "world.adp"
 text/html; charset=UTF-8
 
 % ns_guesstype somefile
 */*

Keywords

configuration, global built-in, mime, return