[PHP] 非公開領域にある pdf をダウンロードさせる

ログインとかしてないと pdf をダウンロードできないようにしようと思って、pdf を非公開領域に置いて、いかのようなコードを書いたんですよ。


<?php
$mime_type = 'application/pdf';
$file_path = '/path/to/pdf';
header('Content-Disposition: inline; filename="'.basename($file_path).'"');
header('Content-Length: '.filesize($file_path));
header('Content-Type: '.$mime_type);
readfile($file_path);

view raw

gistfile1.php

hosted with ❤ by GitHub

そしたら https な環境で IE 8 からダウンロードできないってクレームががが…

IE5 – IE8 では https 環境で Cache-control:no-cache へっだが付いてるとダウンロードできないんですって…
Internet Explorer file downloads over SSL do not work with the cache control headers

そこで以下のようになおしました。


<?php
$mime_type = 'application/pdf';
$file_path = '/path/to/pdf';
// for IE 8 ( see http://support.microsoft.com/kb/323308/ja )
$expires = 30;
header("Last-Modified: ". gmdate("D, d M Y H:i:s", filemtime($file_path)). " GMT");
header("Expires: " . date(DATE_RFC1123, time() + $expires));
header("Pragma: cache");
header("Cache-Control: max-age={$expires}");
header("Cache-Control: cache");
// for IE 8
header('Content-Disposition: inline; filename="'.basename($file_path).'"');
header('Content-Length: '.filesize($file_path));
header('Content-Type: '.$mime_type);
readfile($file_path);

view raw

gistfile1.php

hosted with ❤ by GitHub

IE8めー


Posted

in

by

Tags:

Comments

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト /  変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト /  変更 )

%s と連携中

%d人のブロガーが「いいね」をつけました。