Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.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在浏览器中播放AIFF文件的正确HTTP头_Php_Audio_Http Headers_Aiff - Fatal编程技术网

使用PHP在浏览器中播放AIFF文件的正确HTTP头

使用PHP在浏览器中播放AIFF文件的正确HTTP头,php,audio,http-headers,aiff,Php,Audio,Http Headers,Aiff,我正试图将一些AIFF音频文件放在PHP站点的登录墙后面(即web根目录之外)。第一个挑战是并非所有浏览器都支持AIFF,但这是意料之中的——请看,现在我使用Safari进行测试,因为它支持AIFF 我不明白为什么Safari对同一文件的两个版本有不同的处理方式。对于直接文件,它会提示玩家并使其工作。对于流文件,播放机不工作 定期下载 以下是我直接下载文件时(即,如果我临时将文件放入web根目录中进行测试)的标题: curl-vhttp://audio.app/Morse.aiff -o/dev

我正试图将一些AIFF音频文件放在PHP站点的登录墙后面(即web根目录之外)。第一个挑战是并非所有浏览器都支持AIFF,但这是意料之中的——请看,现在我使用Safari进行测试,因为它支持AIFF

我不明白为什么Safari对同一文件的两个版本有不同的处理方式。对于直接文件,它会提示玩家并使其工作。对于流文件,播放机不工作

定期下载

以下是我直接下载文件时(即,如果我临时将文件放入web根目录中进行测试)的标题:

curl-vhttp://audio.app/Morse.aiff -o/dev/null
%总接收百分比%x平均速度时间电流
数据加载上载总左速度
0 0 0 0 0 0 0--:-:-:-:---:-:---:---0*正在尝试192.168.10.10。。。
*已连接到audio.app(192.168.10.10)端口80(#0)
>GET/Morse.aiff HTTP/1.1
>主持人:audio.app
>用户代理:curl/7.49.1
>接受:*/*
>
通过PHP

下面是通过PHP脚本(名为source.PHP)流式传输文件时的标题:

curl-vhttp://audio.app/source.php?file=Morse.aiff -o/dev/null
%总接收百分比%x平均速度时间电流
数据加载上载总左速度
0 0 0 0 0 0 0--:-:-:-:---:-:---:---0*正在尝试192.168.10.10。。。
*已连接到audio.app(192.168.10.10)端口80(#0)
>GET/source.php?file=Morse.aiff HTTP/1.1
>主持人:audio.app
>用户代理:curl/7.49.1
>接受:*/*
>
这些头几乎是相同的——我能看出的唯一区别是它们的顺序和我的本地开发框用于ETag值的散列算法

下面是测试PHP脚本(名为source.PHP),我正在使用它来流式处理同一个文件(位于webroot之上):

//改编自http://php.net/manual/en/function.readfile.php
$filename=(isset($\u GET['file'])?$\u GET['file']:null;
// 
$file=dirname(dirname(_文件))。/audio/。$filename;
//从curl头模拟AIFF头(不起作用!)
$content\u length=文件大小($file);
$last_modified=date(“D,dm Y H:i:s”,filemtime($filename)).'GMT';
标题(“HTTP/1.1200ok”);
标题(“内容类型:应用程序/八位字节流”);
标题(“内容长度:”.$Content\u Length);
标题('Last-Modified:'.$Last_Modified);
//尝试执行与NGINX…md5_file()相同的操作可能会奏效
$etag=sprintf(“\%xT-%xO\”,filemtime($filename),$content\u length);
header(“ETag:$ETag”);//准确地引用它
标题(“接受范围:字节”);
//输出文件
readfile($file);
预期的行为是浏览器会将两个版本视为相同的。在我的示例HTML页面(改编自)中,只有直接下载有效——通过PHP发送的文件版本不会播放。当我直接在浏览器中点击两个文件时,会发生相同的行为

<!DOCTYPE html>
<html>
<body>
<h2>From Stream</h2>
<audio controls>
  <source src="/source.php?file=Morse.aiff&breakcache=<?php print uniqid(); ?>" type="audio/x-aiff">
    Your browser does not support the audio element.
</audio>
<hr/>
<h2>Direct Downloads</h2>
<audio controls>
  <source src="/Morse.aiff" type="audio/x-aiff">
  Your browser does not support the audio element.
</audio>
</body>
</html>

从溪流
curl -v http://audio.app/source.php?file=Morse.aiff -o /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 192.168.10.10...
* Connected to audio.app (192.168.10.10) port 80 (#0)
> GET /source.php?file=Morse.aiff HTTP/1.1
> Host: audio.app
> User-Agent: curl/7.49.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.8.0
< Date: Sun, 06 Nov 2016 03:36:46 GMT
< Content-Type: application/octet-stream
< Content-Length: 55530
< Connection: keep-alive
< Last-Modified: Sat, 05 Nov 2016 21:51:02 GMT
< ETag: "581e5446T-d8eaO"
< Accept-Ranges: bytes
<
{ [8431 bytes data]
100 55530  100 55530    0     0  4915k      0 --:--:-- --:--:-- --:--:-- 5422k
* Connection #0 to host audio.app left intact
// Adapted from http://php.net/manual/en/function.readfile.php
$filename = (isset($_GET['file'])) ? $_GET['file'] : null;
// <do sanitization here>
$file = dirname(dirname(__FILE__)).'/audio/' . $filename;

// Mimicking AIFF headers from curl headers (does not work!)
$content_length = filesize($file);
$last_modified = date("D, d M Y H:i:s", filemtime($filename)). ' GMT';
header("HTTP/1.1 200 OK");
header("Content-type: application/octet-stream");
header('Content-Length: ' . $content_length);
header('Last-Modified: ' .$last_modified);
// attempts to do the same thing as NGINX... md5_file() would probably work
$etag = sprintf("\"%xT-%xO\"", filemtime($filename), $content_length);
header("ETag: $etag"); // quoting it exactly
header("Accept-Ranges: bytes");

// Output the file
readfile($file);
<!DOCTYPE html>
<html>
<body>
<h2>From Stream</h2>
<audio controls>
  <source src="/source.php?file=Morse.aiff&breakcache=<?php print uniqid(); ?>" type="audio/x-aiff">
    Your browser does not support the audio element.
</audio>
<hr/>
<h2>Direct Downloads</h2>
<audio controls>
  <source src="/Morse.aiff" type="audio/x-aiff">
  Your browser does not support the audio element.
</audio>
</body>
</html>