在 MySQL 回声输出上播放位于服务器文件夹中的 .MP3 文件

问题描述 投票:0回答:1

我目前有一个表单,用户可以在其中上传详细信息,包括 MP3 文件。详细信息存储在 MySQL 数据库中,MP3 文件存储在我的服务器文件夹

/orgs/user/uploads
(URL
domain.com/orgs/user/uploads
)中,MP3 文件名存储在数据库中。

我正在尝试使用下面的脚本输出它,并且似乎音频播放器功能工作正常,但是我一生都无法弄清楚如何从其中的服务器调用文件位置,因此它当前显示没有音频的音频控件。

  if ($row ['approved'] == "1" && $row  ['service'] == "Army" )
{
    
      echo "<div class='speech-bubble'>";
       echo "<p> <strong>" . $row['first_name'] . ' ' . $row['initial'] .  "</strong></p>"; // Replace 'column_name' with the actual column name from your table
           echo "<p>" . $row['service'] . "</p>"; 
           echo "<p>" . $row['year'] . "</p>"; 
           echo "<p>" . $row['story_text'] . "</p>"; 
           echo '<audio controls>';
           echo '<source src="data:audio/mp3;base64,'.base64_encode($row['name']).'">';
           echo '</audio>';

在“script src”部分放置不同版本的服务器路径和URL

php mysql
1个回答
0
投票

你必须先获取文件的内容然后使用它。如果您仅在表中保存文件名,您也可以检查文件是否存在


$folder= "/orgs/user/uploads/"; // upload folder

if ($row ['approved'] == "1" && $row  ['service'] == "Army" ){

   if(file_exists($folder.$row['name']) ){ // check for file
 
      $file = file_get_contents($folder.$row['name']); // get file contents

      echo "<div class='speech-bubble'>";
       echo "<p> <strong>" . $row['first_name'] . ' ' . $row['initial'] .  "</strong></p>"; // Replace 'column_name' with the actual column name from your table
           echo "<p>" . $row['service'] . "</p>"; 
           echo "<p>" . $row['year'] . "</p>"; 
           echo "<p>" . $row['story_text'] . "</p>"; 
           echo '<audio controls>';
           echo '<source src="data:audio/mp3;base64,'.base64_encode($file).'">';
           echo '</audio>';
   } // if file
}// if approved

如果您只在表中保存了文件名,您也可以直接从 url 调用文件,如下所示

$folder= "/orgs/user/uploads/"; // upload folder

if ($row ['approved'] == "1" && $row  ['service'] == "Army" ){

   if(file_exists($folder.$row['name']) ){ // check for file
 
      $file = file_get_contents($folder.$row['name']); // get file contents

      echo "<div class='speech-bubble'>";
       echo "<p> <strong>" . $row['first_name'] . ' ' . $row['initial'] .  "</strong></p>"; // Replace 'column_name' with the actual column name from your table
           echo "<p>" . $row['service'] . "</p>"; 
           echo "<p>" . $row['year'] . "</p>"; 
           echo "<p>" . $row['story_text'] . "</p>"; 
           echo '<audio controls>';
           echo '<source src="data:audio/mp3;base64,'.base64_encode($file).'">';
           echo '</audio>';
   } // if file
}// if approved

© www.soinside.com 2019 - 2024. All rights reserved.