php – 如果设置了部分内容标头,则Google Chrome无法完成请求

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如果设置了部分内容标头,则Google Chrome无法完成请求脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用HTTP / 1.1 Partial Content标头将mP3文件流式传输到SoundManager,以便为我的媒体文件提供一些保护,并允许用户搜索到轨道的不同位置.我在IE7-10,Firefox,Safari和opera中使用过的代码,但拒绝在GOOGLE Chrome中使用.如果我要删除部分内容标题,它将播放该文件但不允许用户搜索.

在检查Chrome开发工具中的网络选项卡时,有2个请求,其中一个处于暂挂状态,另一个处于取消状态.这两个请求的大小都是13B.我要播放的文件是9.11MB.

下面是我用来设置标题和读取文件代码.

$name = $_GET['name'];
        $file_size = filesize($file);
        $file = "C:\xampp\htdocs\..\PRotected\music\testsong.mp3"; // song,user and all other checks are PErformed before this to get the file path.
        $etag = md5(uniqid(rand(),true));

        $open_file = fopen($file,'rb');
        if( !$open_file ) throw new Exception('Could not open file');

        $start = 0;
        $end = $file_size - 1;

        if( isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE']) ) {
            $range = explode('-',substr($_SERVER['HTTP_RANGE'],strlen('bytes=')));

            $start = $range[0];
            if( $range[1] > 0 ) $end = $range[1];

            header('HTTP/1.1 206 Partial Content');
            header('status: 206');
            header('Accept-Ranges: bytes');
            header('Content-Range: ' . $_SERVER['HTTP_RANGE'] . '/' . $file_size);
            header('Content-Length: ' . ($end - $start + 1));
        } else header('Content-Length: ' . $file_size);

        header('Content-type: ' . $content_type);
        if( $download ) header('Content-DisposITion: attachment; filename="' . $name . '"');
        header('Last-Modified: ' . date('D,d M Y H:i:s \G\M\T',filemtime($file)));

        header('ETag: "' . $etag . '"');
        header('Expires: 0');
        header('Pragma: no-cache');
        header('Cache-Control: must-revalidate');

        if( $start > 0 ) fseek($open_file,$start);

        $bytes_position = $start;
        while( !feof($open_file) &amp;& $bytes_position <= $end ) {
            if( connection_aborted() || connection_status() != 0 ) throw New Exception('Connection Aborted');

            $chunk = 1048000;
            if( $bytes_position + $chunk > $end + 1 ) $chunk = $end - $bytes_position + 1;

            $chunk = fread($open_file,$chunk);
            if( !$chunk ) throw New Exception('Could not read file');

            print($chunk);
            flush();

            $bytes_position += $chunk;
        }

        fclose($open_file);
我很确定你遇到的问题是内容范围标题

试试这个

header('Content-Range: bytes ' . $start . '-' . $end . '/' . $file_size);

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如果设置了部分内容标头,则Google Chrome无法完成请求全部内容,希望文章能够帮你解决php – 如果设置了部分内容标头,则Google Chrome无法完成请求所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。