3

「PHPWord」を使用してdocxファイルをダウンロードしようとしています。

ファイルをサーバーに保存しようとすると、正常に動作します。ただし、ヘッダーを追加してダウンロードすると、ファイルが破損した形式で表示されます。

注:openOfficeを使用して開いています。

ここに私のコードがあります:

 $document->save($doc);
 header('Content-Description: File Transfer');
 header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
 header("Content-Disposition: attachment; filename=CV.docx");
 header('Content-Transfer-Encoding: binary');
 header('Expires: 0');
 header('Cache-Control: must-revalidate');
 header('Pragma: public');
 header('Content-Length: ' . filesize($doc));
 readfile($doc);

何が問題なのか教えてください。

4

1 に答える 1

6

推測してみましょう:

プログラムは、sを送信する前に何らかのテキストを出力します (手動で何もしない場合は、php 警告である可能性があり、それも出力としてカウントされます)。したがって、ファイルの実際の出力では、単純なテキスト エディターで開くと(拡張子の名前を変更してメモ帳で開くだけです)、最初の行は次のようになります。headerechotxt.txt

Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23

...そして、docファイルの残りの部分。もちろん、それは破損しています。

その場合は、 s の前に何も出力しないでください。header

于 2014-09-17T07:31:36.420 に答える