0

このようなことはできますか?

ob_start();
header("Content-Type: application/msword");
header("Content-Disposition: attachment; filename=".$title);
header("Content-Length: ".$size);
header("Content-Transfer-Encoding: binary");
readfile($path);
ob_end_flush();

現在、ダウンロードする正しい名前のファイルを取得していますが、ファイルの内容は、上記のコードを含む php からの html 出力です。

$path が正しいことを確認しました。悪い $size はこれを引き起こしますか? また、ob_start() の直後に ob_flush() を試みました。

編集: 以下の現在のコード:

if (file_exists($path)){
    ob_start();
    header("Content-Type: application/msword");
    header("Content-Disposition: attachment; filename=".$title);
    header("Content-Length: ".filesize($path));
    header("Content-Transfer-Encoding: binary");
    readfile($path);
    ob_end_flush();
    die();
    } else die ("Not Found");

上記のコードは同じ問題を引き起こします。ファイルがダウンロードされ、正しく名前が付けられ、明らかに存在し、正しいサイズですが、ファイルの内容はページの html 出力です。

前もって感謝します

これが(誤って)ダウンロードされるファイルの最後にあることに気付きました:「int(14)コンテンツ」

しかし、コードのどこにも意図的にこれを出力していません。

4

1 に答える 1

1

たぶん、いくつかのデバッグが役立つかもしれません:

if (file_exists($path)) {
  ob_start();
  header("Content-Type: application/msword");
  header("Content-Disposition: attachment; filename=".$title);
  header("Content-Length: ".filesize($path));
  header("Content-Transfer-Encoding: binary");
  readfile($path);
  ob_end_flush();
  die(); // To not send the HTML afterwards
} else die("Not found");
于 2012-10-31T03:07:42.627 に答える