0

在下面的代码中:在 Safari 中进行部分下载。我不断收到错误:

Failed to load resource: Plug-in handled load

我进入if (isset($_SERVER['HTTP_RANGE']))分支。Error_Log 验证这一点。我检查了谷歌浏览器以确保资源实际上被抓取并且确实被抓取并且谷歌浏览器下载了前面提到的 if 语句的 else 语句中的整个文件。

我需要做什么来更正标题?由于“->read(1024)”,流总是以 1024 字节或更少字节被抓取。

$s3 = S3Client::factory([
        'version' => 'latest',
        'region' => 'A Region',
        'credentials' => [
            'key' => "A KEY",
            'secret' => "A SECRET",
        ]
    ]);

//echo "Step 4\n\n";
    
    $bucket = "bucket";
 
    $image_path = $_GET['video_path'];
    
    try {
        
        $result = $s3->getObject([
            'Bucket' => $bucket,
            'Key' => $image_path
        ]);
       

        header("Content-Type: {$result['ContentType']}");
        $fileSize = $result['ContentLength'];
       

        if (isset($_SERVER['HTTP_RANGE'])){ // do it for any device that supports byte-ranges not only iPhone
           // echo "partial";
            error_log("Error message We are good on safari in HTTP_RANGE\n", 3, "error_log");
            // Read the body off of the underlying stream in chunks
           

            header('HTTP/1.1 206 Partial Content');
            header('Accept-Ranges: bytes');
            $start = 0;
            //$theLengthOfTheNextData = mb_strlen($data, '8bit');
            header("Content-Range: bytes 0-1023/$fileSize");
            header("Content-Length: 1024");
            while ($data = $result['Body']->read(1024))
            {

                //$theLengthOfTheNextData = mb_strlen($data, '8bit');
                //it starts at zero

                //$end = $start + $theLengthOfTheNextData - 1;


                //$theLengthOfTheNextData = mb_strlen(data);
                //echo "Length: $theLengthOfTheNextData\n\n";

                //header("Content-Range: bytes $start-$end/$fileSize");
                //header("Content-Length: $theLengthOfTheNextData");
                //$start = $start + $theLengthOfTheNextData;

                // header("Content-Range: bytes $start-$end/$size");

                set_time_limit(0);
                echo $data;


                // Flush the buffer immediately
                //@ob_flush();

                flush();

            }
            //header("Content-Length: $fileSize");

             //rangeDownload($result['Body'],$fileSize);
        }
        else {
           // echo "Just entire thing";
            error_log("Error message ---- NOT good on safari in HTTP_RANGE\n", 4, "error_log");
            header("Content-Length: ".$fileSize);
            echo $result['Body'];

        }
        //echo "Step 7\n\n";
        //no need to cast as string
        // Cast as a string
        // $body = (string)$result->get('Body');
        //below should actually work too
        // $bodyAsString = (string) $result['Body'];
    } catch (S3Exception $e) {
        echo $e;
        //echo "Step 6B\n\n";
        //grabbing the file was unsuccessful
        $successfulMove = 0;
    }
4

1 回答 1

0

如果有人正在寻找这个问题的答案。您将需要在 S3 函数中使用“registerStreamWrapper”。

参考链接

您也需要向下滚动“tedchou12”评论。有用!!!

于 2021-05-20T04:25:42.263 回答