Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果设置了部分内容标题,则Google Chrome未完成请求_Php_Google Chrome_Http Headers_Streaming - Fatal编程技术网

Php 如果设置了部分内容标题,则Google Chrome未完成请求

Php 如果设置了部分内容标题,则Google Chrome未完成请求,php,google-chrome,http-headers,streaming,Php,Google Chrome,Http Headers,Streaming,我正在尝试使用HTTP/1.1部分内容头将mp3文件流式传输到SoundManager,以允许对我的媒体文件进行一些保护,并允许用户搜索曲目的不同位置。我的代码可以在IE7-10、Firefox、Safari和Opera中使用,但拒绝在Google Chrome中使用。如果我删除部分内容标题,它将播放文件,但不允许用户搜索 当检查Chrome开发工具中的网络选项卡时,有两个请求,一个被挂起,另一个被取消。这两个请求的大小都是13B。我试图播放的文件是9.11MB 下面是我用来设置标题和读取文件的

我正在尝试使用HTTP/1.1部分内容头将mp3文件流式传输到SoundManager,以允许对我的媒体文件进行一些保护,并允许用户搜索曲目的不同位置。我的代码可以在IE7-10、Firefox、Safari和Opera中使用,但拒绝在Google Chrome中使用。如果我删除部分内容标题,它将播放文件,但不允许用户搜索

当检查Chrome开发工具中的网络选项卡时,有两个请求,一个被挂起,另一个被取消。这两个请求的大小都是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) && $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);
$name=$\u GET['name'];
$file\u size=文件大小($file);
$file=“C:\xampp\htdocs\..\protected\music\testsong.mp3”;//在此之前,将执行song、user和所有其他检查以获取文件路径。
$etag=md5(uniqid(rand(),true));
$open_file=fopen($file,'rb');
如果(!$open_file)抛出新异常(“无法打开文件”);
$start=0;
$end=$file_size-1;
如果(isset($\u服务器['HTTP\u范围]])和&!空($\u服务器['HTTP\u范围]])){
$range=explode('-',substr($_SERVER['HTTP_range'],strlen('bytes='));
$start=$range[0];
如果($range[1]>0)$end=$range[1];
标题(“HTTP/1.1 206部分内容”);
标题(“状态:206”);
标题('Accept-Ranges:bytes');
标头('Content-Range:'.$\u服务器['HTTP\u Range'.]./'.$file\u大小);
标题('Content-Length:'。($end-$start+1));
}else头('Content-Length:'.$file\u size);
标题(“内容类型:”.$Content\u Type);
if($download)标题('Content-Disposition:attachment;filename=“”.$name.”);
标题('Last-Modified:'.date('D,dm Y H:i:s\G\M\T',filemtime($file));
标题('ETag:'.$ETag.'”);
标题('Expires:0');
标题('Pragma:no cache');
标头(“缓存控制:必须重新验证”);
如果($start>0)fseek($open_file,$start);
$bytes\u position=$start;
而(!feof($open_file)&&$bytes_position$end+1)$chunk=$end-$bytes_position+1;
$chunk=fread($open_file,$chunk);
如果(!$chunk)抛出新异常('无法读取文件');
打印(块);
冲洗();
$bytes\u position+=$chunk;
}
fclose($open_file);

我很确定您遇到的问题是内容范围标题

试试这个

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

我在MP4流媒体方面也有同样的问题。您是否有机会解决urs?请检查您试图流式传输的文件的比特率。我也有同样的问题,但对我来说,当我开始将所有音频文件压缩到128kbps时,请求完成得很好。