通过PHP发送要与HTML5的音频标签一起使用的mp3文件在mp3中间失败 [英] Sending mp3 file through PHP to be used with the audio tag of HTML5 fails in the middle of the mp3

查看:98
本文介绍了通过PHP发送要与HTML5的音频标签一起使用的mp3文件在mp3中间失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过PHP脚本发送mp3文件,以便在HTML中隐藏文件的路径.一切工作都很好,除了进入mp3 chrome的一半,这给了我GET [URL]错误.而mp3的其余部分仅为空白.音频标签认为它仍在读取文件,但没有声音.

I'm trying to send my mp3 files through a PHP script in order to hide the path to the file in the HTML. Everything works perfectly fine except half way into the mp3 chrome gives me a GET [URL] error. And the rest of the mp3 is just blank. The audio tag thinks it's still reading the file, but there is no sound.

这就是我从php发送mp3文件的方式:

This is how I'm sending the mp3 file from php:

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标头.这似乎使事情变得更好.该问题仍然存在,但没有那么频繁. :-/

I added the Etag header. It seems to make things better. The problem still occurs but not as often. :-/

Edit2:我注意到发送到我的PHP文件的请求标头与直接发送到mp3文件的标头不同.实际上,有一个HTTP_RANGE请求发送到mp3文件,但是当我尝试从PHP中获取内容时,此范围请求丢失了. (这是在Chrome中).知道为什么会这样吗?

I have noticed that the request headers sent to my PHP file is different from the headers sent directly to the mp3 file. Actually there's a HTTP_RANGE request sent to the mp3 file, but this range request is missing when I try to fetch things from PHP. (This is in Chrome). Any idea why this might be ?

推荐答案

我知道这是一篇旧文章,但是我能够使用两个文件以及index.php和example.php来使它工作.

I know this is an old post but I was able to get this working with two files and index.php and example.php.

这是example.php,它正在检查数据库中的哈希值和使用过的字段,以了解该文件之前是否已使用过

this is example.php that's checking a databse for a hash and a used field to know if the file has been used before

<?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");
  }

,这里是带有音频标签的index.php,它将允许播放但不能下载mp3.

and here is index.php with the audio tag that will allow playing but not downloading of the mp3.

<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>

有一个数据库表,其中仅包含一个哈希表和一个用于此示例的used字段

there is a database table with just a hash and a used field for this example to work

这篇关于通过PHP发送要与HTML5的音频标签一起使用的mp3文件在mp3中间失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆