在MP3的中间,通过PHP发送MP3文件,与HTML5的音频标签一起使用失败。
我试图通过PHP脚本发送mp3文件,以便在HTML中隐藏文件的路径。一切都很好,除了mp3浏览器的一半给了我一个GET[URL]错误。剩下的mp3都是空白的。音频标签认为它仍在读取文件,但没有声音 以下是我从php发送mp3文件的方式:在MP3的中间,通过PHP发送MP3文件,与HTML5的音频标签一起使用失败。,php,mp3,html5-audio,Php,Mp3,Html5 Audio,我试图通过PHP脚本发送mp3文件,以便在HTML中隐藏文件的路径。一切都很好,除了mp3浏览器的一半给了我一个GET[URL]错误。剩下的mp3都是空白的。音频标签认为它仍在读取文件,但没有声音 以下是我从php发送mp3文件的方式: if (file_exists($filename)) { header("Content-Transfer-Encoding: binary"); header("Content-Type: audio/mpeg"); header('Conte
if (file_exists($filename)) {
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/mpeg");
header('Content-length: ' . filesize($filename));
header('Content-Disposition: inline; filename="' . $filename . '"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($filename);
exit;
}
else {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
echo "no file";
}
编辑:我添加了Etag标题。这似乎使事情变得更好。该问题仍然存在,但不是经常出现-/
Edit2:我注意到发送到PHP文件的请求头与直接发送到mp3文件的头不同。实际上,有一个HTTP_范围请求发送到mp3文件,但是当我尝试从PHP获取内容时,这个范围请求丢失了。(这是镀铬的)。知道为什么会这样吗?我知道这是一篇老文章,但我能用index.php和example.php两个文件来完成这篇文章 这是example.php,它正在检查数据库中的散列和已用字段,以了解该文件以前是否被使用过
<?php
if(isset($_GET['hash'])){
$mysqli = new mysqli('host_here','user_here','password_here','database_here');
$result = $mysqli->query("select * from hashes where hash = '{$_GET['hash']}' limit 1");
$row = $result->fetch_array();
}
if(isset($row['used']) && $row['used'] == 0){
$mysqli->query("update hashes set used=1 where hash = '{$_GET['hash']}'");
$filename = "example.mp3";
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/mpeg");
header('Content-length: ' . filesize($filename));
//If this is a secret filename then don't include it.
header('Content-Disposition: inline');
//Otherwise you can add it like so, in order to give the download a filename
//header('Content-Disposition: inline;filename='.$filename);
header('Cache-Control: no-cache');
readfile($filename);
exit;
}
elseif(isset($row['used']) && $row['used'] == 1){
die("you can't just download this dude");
}
您的脚本不支持范围,chrome中的HTML 5标记可能会请求范围。使脚本支持范围更广,或尝试使用较小的文件。请参见:。我的文件不是很大。平均大小小于10MB。最大的可能是30MB。默认的内容范围是什么?我可以将其设置为50MB吗?@hakre,这应该是一个答案。@hakre,在做了一点挖掘之后,我认为范围不是问题所在,因为当我从html5音频标签收到请求时,isset($\u SERVER['HTTP\u Range'])没有设置。还有其他想法吗?知道如何导航浏览器检查器的人仍然可以使用该路径。不是说这毫无意义,但你并不是真的在“隐藏”这样的东西。
<html>
<body>
<?php
$mysqli = new mysqli('localhost','root','','test_mp3');
$rand = mt_rand();
$hash = md5($rand);
$result = $mysqli->query("insert into hashes (hash,used) values('$hash',0)");
?>
<audio controls>
<source src="example.php?hash=<?=$hash?>" type="audio/mpeg">
</audio>
</body>
</html>