简体   繁体   English

如何在不提供绝对路径的情况下流音频文件

[英]How to stream audio files without giving absolute path

Is there anyway to stream an audio file without giving complete path of the file. 无论如何,是否有流音频文件而没有给出文件的complete path

For example whenever we tried to play mp3 file with HTML5 Audio tag or With javascript, then i have to supply complete path of mp3 file something like this 例如,每当我们尝试使用HTML5音频标签或javascript播放mp3文件时,我都必须提供mp3文件的完整路径,例如

<audio controls>
  <source src="http://example.com/file/hello_name.mp3" type="audio/mpeg">
</audio>

So, i think can we supply an id of the mp3 file instead of complete path of the file which should be something like this: 因此,我认为我们可以提供mp3文件的id而不是文件的complete path ,它应该是这样的:

<audio controls>
  <source src="http://example.com/stream.php?file_id=111" type="audio/mpeg">
</audio>

Typically you'd have stream.php deliver the binary data (eg for file_id == 111 as in your example) by sending the correct headers with header() and the data with readfile() provided that you have a relation between file_id and a physical file in a database or something similar. 通常你必须stream.php通过发送正确的标题传递二进制数据(的file_id == 111例如在你的例子) header()函数 ,并与数据ReadFile的(),前提是你得之间的关系file_id和数据库中的物理文件或类似文件。

Please be careful with readfile() , though, and be aware that it will just output whatever it's told to, so make sure your application doesn't allow someone to tell it to output other stuff like config files with database passwords and such. 不过,请谨慎使用readfile() ,并注意它只会输出所告知的内容,因此请确保您的应用程序不允许有人告诉它输出带有数据库密码等配置文件的其他内容。

Your stream.php file will look like this: 您的stream.php文件将如下所示:

<?php
    // Say you hava a table "file_paths" with two columns file_id and file_path
    $sql = $pdo->prepare("SELECT FROM file_paths WHERE file_id = ?");
    $sql->bindValue(1, $_GET['file_id'], PDO::PARAM_INT);
    $sql->execute();
    $fetch = $sql->fetchObject();

    // retrieve file path using its id
    $path = $fetch->file_path;
    header("Location: " . $path);
?>

And then on target page, add the player by passing file_id to stream.php like this: 然后在目标页面上,通过将file_id传递到stream.php来添加播放器,如下所示:

<audio controls>
  <source src="stream.php?file_id=123" type="audio/mpeg">
</audio>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM