あなたがやろうとしていることは、この質問をチェックすることはお勧めできません。ヘッダーの場所 + コンテンツの配置
Content-Disposition + Location ヘッダー
しかし、それを行うことはできます。それを機能させるには、送信する前に応答全体をバッファリングする必要があります。これは、出力バッファリングで行うことができます
Location
そうしないと、ファイルがダウンロードされる前にブラウザがヘッダーを解釈する可能性があります。どちらにしても大雑把なので、これを行うべきではありません。
「名前を付けて保存」を強制Content-Disposition: attachment;
すると、クライアントがどこにも移動/移動しないようになるため、以下の方法だけで問題ないことに注意してください。
PHPでファイルをストリーミングする
適切な場所に頭脳を持っている人の言葉を引用すると:
// To use header() with 'content-type', why don't you use mime_content_type() function rather than checking the type on the basis of extension?
// Example code:
<?php
$file="test.docx";
header("Pragma: public");
header('Content-disposition: attachment; filename='.$file);
header("Content-type: ".mime_content_type($file));
header('Content-Encoding: identity');
ob_clean();
flush();
readfile($file);
?>
// Use $file to map to whichever type of file.
// Note: the mime types should already be defined in apache settings
ソース: http://www.php.net/manual/en/function.header.php#107581
Content-Transfer-Encoding
HTTP には実際には存在しない元の回答が使用されていることに注意してください。そのソースの下のコメントはそれを説明しています: http://www.php.net/manual/en/function.header.php#107044