Play .MP3 file located in server folder on MySQL echo output

ghz 8months ago ⋅ 97 views

I currently have a form where users upload details including an MP3 file. Details are stored on a MySQL DB and the MP3 file is stored in my server folder /orgs/user/uploads (URL domain.com/orgs/user/uploads) with the MP3 file name stored in the DB.

I'm trying to output it using the below script and seem to have the audio player function working fine, however for the life of me can't figure out how to call the file location from the server in it therefore it's currently displaying the audio controls with no audio.

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

Placed different versions of the server path and URL in "script src" section

Answers

To properly display the audio file stored on your server, you need to provide the correct URL to the audio file in the <source> tag within the <audio> element. Since you mentioned that the MP3 files are stored in the /orgs/user/uploads folder on your server, you can construct the URL dynamically using PHP to point to the correct file location.

Assuming that the file name is stored in the database column name, you can construct the URL like this:

echo '<source src="http://domain.com/orgs/user/uploads/' . $row['name'] . '" type="audio/mp3">';

Here's how you can integrate it into your existing code:

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="http://domain.com/orgs/user/uploads/' . $row['name'] . '" type="audio/mp3">';
    echo '</audio>';
}

Replace domain.com with your actual domain name. This will dynamically generate the URL to your MP3 file stored on the server, allowing it to be played using the HTML5 <audio> element.