mod_xsendfileでダウンロード後にサーバ上のファイル自動削除

mod_xsendfileのホームページ上でリリースされているバージョン0.12には含まれていませんが、GithubのレポジトリのソースではX-Sendfile-Temporaryという拡張ヘッダに対応しています。

Scientific Linux 6.1で実験しました。

mod_xsendfileのインストール

以下の手順でインストールします。

yum install -y httpd-devel
git clone https://github.com/nmaier/mod_xsendfile.git
cd mod_xsendfile
apxs -cia mod_xsendfile.c

実験スクリプト用にApacheの設定ファイルを作成します。

<Directory /var/www/html/xsendfile>
    XSendFilePath /var/www/html/xsendfile/data AllowFileDelete
    <Files out.php>
      XSendFile on
    </Files>
</Directory>

Apache再起動。

/etc/init.d/httpd graceful

実験

実験スクリプト用のフォルダを作ります。

mkdir /var/www/html/xsendfile/data/
chown -R apache:apache /var/www/html/xsendfile

実験用のPHPスクリプトを作成します。

<?php
$path = '/var/www/html/xsendfile/data/file1.txt';
$fname = basename($path);
header("X-Sendfile-Temporary: $path");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$fname\"");

実験用のダウンロードファイルを作成します。

Hello, X-Sendfile-Temporary!

これで、ブラウザで http://your_host_here/xsendfile/out.php を開くとダウンロード後にサーバ上のファイルが削除されました。

今回のはまりポイント

<Directory /var/www/html/xsendfile>
    XSendFilePath /var/www/html/xsendfile/data
    <Files out.php>
      XSendFile on
    </Files>
</Directory>

のようにAllowFileDeleteを忘れていたら、out.phpを開いた時に404 Not Foundエラーになり、Apacheのエラーログには以下のようなエラーが出ていました。

[Sat Mar 03 15:57:07 2012] [error] [client 192.168.11.3] (14)Bad address: xsendfile: cannot open file: (null)

AllowFileDeleteをつければOKでした。