-1

ユーザーがphpを使用してpptxおよびzipファイルをダウンロードできるようにするスクリプトに取り組んでいますが、このスクリプトはブラウザーによって動作が異なります。インターネットで入手できる多くのスクリプトを試しましたが、適切に動作しなかったため、さまざまなスクリプトからチャンクを集めて作成しました。

  1. firefox =>完璧に動作します
  2. Opera => ファイルを HTM ファイルとしてダウンロード
  3. Safari => 0kb ファイル
  4. IE => 古いファイルをキャッチ

私のコード:-

// content type for pptx file 
$ctype = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
$file = "http://www.abc.com/presentation.pptx";
header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: ".$ctype); 
header("Content-Disposition: attachment; filename=\"".basename($file)."\";" ); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".filesize($file)); 

ob_clean(); 
flush(); 
readfile( $file);

上記の一見ランダムな動作を表示する代わりに、すべてのブラウザにファイルを確実にダウンロードさせるにはどうすればよいですか?

編集: 以下は私のために働いたコードです。何が問題なのか、不要なヘッダーまたはファイルパスがわかりませんか? 私は両方の変更を加えましたが、うまくいきました。

$ctype = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
$file = "Presentation3.pptx";
header("Pragma: public"); 
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: ".$ctype); 
header("Content-Disposition: attachment; filename=\"".basename($file)."\";" ); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".filesize($file)); 
readfile( $file); 
4

3 に答える 3

1

ファイルがサーバーにある場合は、URL を使用しないでください。外部ファイルの場合は、最初にサーバーに書き込み、次にユーザーに出力します。

header("Cache-Control: private",false);行も削除します。header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');の代わりに使用しheader("Expires: 0");ます。それはいつもこのように私のために働いた:)

また、削除してみてくださいob_clean(); flush();(ファイルを送信する前にフラッシュまたはクリーニングするものがないため)。

于 2013-04-16T20:07:22.593 に答える