mod_xsendfile

Overview

mod_xsendfile module handles X-SENDFILE headers registered by the original output handler. If the module finds X-SENDFILE header, it discards the output and instead sends the file specified by that header using Ape core functions.

It is useful to:

  • Speed-up statics downloading for CMS (by avoiding reading/sending file via script engine)
  • Check user ability to download (e.g. checking quota in database)
  • Record downloads statistics (advanced download counter)
  • Flexible configuration of 'Expires' header

Quick start

Having mod_xsendfile enabled, you may use the following code to initiate downloading (not processing) of hello.txt

httpd.conf:

XSendFilePath c:/inetpub/wwwroot/data

.htaccess:

XSendFile On

PHP script:

<?php
//set proper Content-Type
header('Content-Type: plain/text');
//force download box with the filename hello.txt
header('Content-Disposition: attachment;filename=hello.txt');
//file is located at c:/inetpub/wwwroot/data/hello.txt
header('X-Sendfile: c:/inetpub/wwwroot/data/hello.txt');
?>

Related articles and topics

Directives

Name Context Description
XSendFile S V D .h enables processing of X-SENDFILE header
XSendFilePath S V D .h specifies the allowed paths to serve files using mod_xsendfile
XSendFileAllowAbove S V D .h allows sending files above Request path (deprecated now, use XSendFilePath instead)

XSendFile

When On , enables processing of X-SENDFILE header.

Syntax

XSendFile On|Off

Description

XSendFile directive sends the file specified in X-SENDFILE header instead of the handler output.
If the response lacks the X-SENDFILE header, nothing will happen.

By default mod_xsendfile will allow downloads from the directory of the current request or below. To enable downloads from other locations use XSendFilePath directive.

Example

<Files out.php>
XSendFile On
</Files>

XSendFilePath

Specifies the allowed paths to serve files using mod_xsendfile

Syntax

XSendFilePath path ...

Description

By default mod_xsendfile will allow downloads from the directory of the current request or below. XSendFilePath enables downloads from any locations. Paths must be absolute, e.g. starting with drive letter: c:\ or d:\.

Example

<VirtualHost www.example.com>
  XSendFilePath c:\inetpub\downloads d:\site\data\images
</Location>

XSendFileAllowAbove

Deprecated! Allows or disallows sending files above Request path.

Syntax

XSendFileAllowAbove On|Off

Description

XSendFileAllowAbove directive, when set, allows sending files not below the path of the Request (this refers to the request URI not the translated path).

Example

<VirtualHost www.example.com>
  XSendFileAllowAbove On
</VirtualHost>