class FileDownload


Routines to send a file to the browser.
Author: Umberto Salsi <>
Copyright: Copyright 2018 by di Umberto Salsi
Version: $Date: 2018/12/22 23:27:35 $

Class constants


public APPLICATION_OCTET_STREAM = "application/octet-stream"

Default generic MIME type for unknown files.



public static string getFilenameExtension(string $path)

Returns the extension of the file name. The extension is anything that follows the last dot in the path and containing latin letters, digits and underscore.
Return: Filename extension converted to uppercase letters, or the empty string if no suitable extension is found.


public static string getTypeFromExtension(string $ext) throws RuntimeException

Returns a guess for the type of the file based on the given file name extension.
$ext - File name extension, for example "txt".
Return: Corresponding MIME type.


public static string getTypeFromFilename(string $filename) throws RuntimeException

Returns a guess for the type of the file based on its filename extension. Note that this function relies only on the file name extension alone, then it does not pretend a corresponding file does actually exist. On the contrary, the standard mime_content_type() does really access the file with that name, which might not really exist (user's submitted files should always be saved under some safe, harmful temporary file name).
Return: Best guessed MIME type of the file.


public static string[string] parseMimeTypesFile(string $path) throws ErrorException

Parses the Apache MIME types file.
Return: Associative array that maps lower-case file name extensions to their corresponding MIME type.


public static void sendFile(string $path) throws ErrorException

Sends the raw content of a file. A previous call to sendHeaders() is recommended if not mandatory to properly set the type and suggested file name.
$path - Path of the file on the server. This name has nothing to do with the name suggested to the user.
ErrorException - Failed access to the file system.


public static void sendHeaders(string $name, string $type, boolean $isAttachment)

Sends to the browser the headers to send a generic content.
$name - Suggested name of the file, UTF-8 encoded.
$type - MIME type. For "text/plain" and "text/html" PHP automatically adds the default_charset as specified in the configuration file; you may want to override that setting by specifying your own encoding, for example "text/plain; charset=UTF-8".
$isAttachment - True if the browser should prompt the user to save the content locally, rather than display it immediately.

Private properties: $cached_extensions


PHP Version: 7

Required modules: core, file, pcre, phpinfo, spl

Required packages:


Generated by PHPLint Documentator