0

そのため、Amazon S3 にファイルを保存しています。私の顧客はこれらのファイルを当社の Web サイトからダウンロードし、ダウンロードをクリックすると、情報が当社の download.php ページに送信されますが (顧客にはこのページは表示されません)、そこで PHP を使用してファイル名とパスを取得します (以下のコード)。 . しかし、私たちが抱えている問題は、ブラウザにファイル サイズを伝えていないため、顧客がダウンロードしているときに「残り時間不明」と表示されることです。download.php ページがその情報を取得して単独で渡すことができるようにするにはどうすればよいですか?

<?php

$file_path = "http://subliminalsuccess.s3.amazonaws.com/";
$file_name = $_GET['download'];
$file = file_get_contents('$file_name');

header('application/force-download');
header( 'Content-Type: application/octet-stream' );
header('Content-Disposition: attachment; filename="'.$file_name.'"');

$pos = strpos($file_name, "http");

if ($pos !== false && $pos == 0)
{
readfile($file_name);
} else readfile($file_path.$file_name);

?>
4

1 に答える 1

1

それはとても簡単です。file_get_contents() を実行すると、strlen() でファイル サイズを取得できます。次に、応答で Content-Length ヘッダーを送信します。

<?php

$file_path = 'http://subliminalsuccess.s3.amazonaws.com/';
$file      = trim($_GET['download']);
$file_name = $file_path.$file;

$file_contents = file_get_contents($file_name)
    OR die('Cannot get the file: '.$file);

header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream');
header('Content-Length: '.strlen($file_contents));
header('Content-Disposition: attachment; filename="'.basename($file).'"');

echo $file;

ところで、コードには非常に多くの間違いがあります。たとえば、ファイルを 2 回読み取ります。1 回目は file_get_contents() を使用し、2 回目は readfile() を使用します。$file_name 変数に URI がありません。file_get_contents('$file_name') も間違っています。

また、着信 URL をチェックせずに readfile() を実行するだけで、誰かが URL をスクリプトに渡す可能性があるため、適切ではありません...

于 2012-05-25T01:38:14.310 に答える