ファイルを強制的にダウンロードする次の機能があります。
static public function download($file, $options=array()) {
$content = (isset($options['content'])) ? $options['content'] : '';
$contentType = (isset($options['contentType'])) ? $options['contentType'] : '';
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.File::filename($file));
header('Content-Type: '.$contentType);
header('Content-Transfer-Encoding: binary');
if ($content!='') {
echo $content;
} else {
readfile($file);
}
}
PDF ファイルと contentType = "application/pdf" を送信します。問題は、ダウンロードした PDF ファイルを開こうとすると、「このドキュメントを開くときにエラーが発生しました。ファイルが壊れている可能性があります」と表示されることです。元のファイルを開くことができ、まったく同じに見えるので奇妙です(ファイル名、サイズなど)