12

まず、以前に同様の質問があったことを認識しています。

件名は質問をほとんど説明していますが、それでも、

ファイルは別のサーバーでホストされており、ユーザーは私のスクリプトを介してファイルをダウンロードし、ストリーミングされます...

しかし問題は、ユーザーが一時停止すると再開できないことです...解決策はありますか?

4

3 に答える 3

17

Accept-Rangesを使用して独自のダウンロード スクリプトを実装してみることができます。Content-Range概念の説明は次のとおりです。

set_time_limit(0);
$download = new ResumeDownload("word.dir.txt", 50000); //delay about in microsecs 
$download->process();

インターネット ダウンロード マネージャーの使用

始める

始める

一時停止

一時停止

一時停止状態

一時停止状態

履歴書

履歴書

終了した

ここに画像の説明を入力

使用クラス

class ResumeDownload {
    private $file;
    private $name;
    private $boundary;
    private $delay = 0;
    private $size = 0;

    function __construct($file, $delay = 0) {
        if (! is_file($file)) {
            header("HTTP/1.1 400 Invalid Request");
            die("<h3>File Not Found</h3>");
        }

        $this->size = filesize($file);
        $this->file = fopen($file, "r");
        $this->boundary = md5($file);
        $this->delay = $delay;
        $this->name = basename($file);
    }

    public function process() {
        $ranges = NULL;
        $t = 0;
        if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_SERVER['HTTP_RANGE']) && $range = stristr(trim($_SERVER['HTTP_RANGE']), 'bytes=')) {
            $range = substr($range, 6);
            $ranges = explode(',', $range);
            $t = count($ranges);
        }

        header("Accept-Ranges: bytes");
        header("Content-Type: application/octet-stream");
        header("Content-Transfer-Encoding: binary");
        header(sprintf('Content-Disposition: attachment; filename="%s"', $this->name));

        if ($t > 0) {
            header("HTTP/1.1 206 Partial content");
            $t === 1 ? $this->pushSingle($range) : $this->pushMulti($ranges);
        } else {
            header("Content-Length: " . $this->size);
            $this->readFile();
        }

        flush();
    }

    private function pushSingle($range) {
        $start = $end = 0;
        $this->getRange($range, $start, $end);
        header("Content-Length: " . ($end - $start + 1));
        header(sprintf("Content-Range: bytes %d-%d/%d", $start, $end, $this->size));
        fseek($this->file, $start);
        $this->readBuffer($end - $start + 1);
        $this->readFile();
    }

    private function pushMulti($ranges) {
        $length = $start = $end = 0;
        $output = "";

        $tl = "Content-type: application/octet-stream\r\n";
        $formatRange = "Content-range: bytes %d-%d/%d\r\n\r\n";

        foreach ( $ranges as $range ) {
            $this->getRange($range, $start, $end);
            $length += strlen("\r\n--$this->boundary\r\n");
            $length += strlen($tl);
            $length += strlen(sprintf($formatRange, $start, $end, $this->size));
            $length += $end - $start + 1;
        }
        $length += strlen("\r\n--$this->boundary--\r\n");
        header("Content-Length: $length");
        header("Content-Type: multipart/x-byteranges; boundary=$this->boundary");
        foreach ( $ranges as $range ) {
            $this->getRange($range, $start, $end);
            echo "\r\n--$this->boundary\r\n";
            echo $tl;
            echo sprintf($formatRange, $start, $end, $this->size);
            fseek($this->file, $start);
            $this->readBuffer($end - $start + 1);
        }
        echo "\r\n--$this->boundary--\r\n";
    }

    private function getRange($range, &$start, &$end) {
        list($start, $end) = explode('-', $range);

        $fileSize = $this->size;
        if ($start == '') {
            $tmp = $end;
            $end = $fileSize - 1;
            $start = $fileSize - $tmp;
            if ($start < 0)
                $start = 0;
        } else {
            if ($end == '' || $end > $fileSize - 1)
                $end = $fileSize - 1;
        }

        if ($start > $end) {
            header("Status: 416 Requested range not satisfiable");
            header("Content-Range: */" . $fileSize);
            exit();
        }

        return array(
                $start,
                $end
        );
    }

    private function readFile() {
        while ( ! feof($this->file) ) {
            echo fgets($this->file);
            flush();
            usleep($this->delay);
        }
    }

    private function readBuffer($bytes, $size = 1024) {
        $bytesLeft = $bytes;
        while ( $bytesLeft > 0 && ! feof($this->file) ) {
            $bytesLeft > $size ? $bytesRead = $size : $bytesRead = $bytesLeft;
            $bytesLeft -= $bytesRead;
            echo fread($this->file, $bytesRead);
            flush();
            usleep($this->delay);
        }
    }
}

使用ファイル

于 2013-04-06T18:26:05.367 に答える
1

これの目的は何ですか?URLのみを非表示にするか、メンバーにダウンロードを許可するだけですか?

あなたがそれを説明した方法は、少しトリッキーです...

  1. スクリプトのダウンロード元のリモート サーバーは、ダウンロードの再開をサポートしている必要があります。
  2. あなたのphpスクリプトは「Accept-Range」ヘッダーをチェックし、それをリモートサーバーに渡す必要があります(ソケットを使用するのが最善の選択肢だと思います)ので、スクリプトは実際にプロキシとして機能します。
于 2013-04-03T15:20:46.073 に答える
1

PHP を使用してファイルを提供している場合は、すべての再開ロジックを自分で実装する必要があります。

Accept-Rangesを送信し、 Rangesに適切に応答する必要があります。

それは仕事の塊です。使いやすいかもしれませんmod_proxy

于 2009-12-12T19:30:16.777 に答える