-1

Similar types of questions are already asked but none is helpful. I want to display working audio file (.mp3 or .wav) in php page from mysql database. I tried my best but the audio file is not playing., #code1

<html>
<form method="post" action="searchbook.php">
<div class="search">
<input type="text" name="searchword" class="searchTerm">
<button type="submit" class="searchButton">SEARCH</button>
</div>
</form>
</html>

From above file I passed user entered value to below searchbook.php file, #code2

<html>
$searchword=$_POST['searchword'];
$con=mysqli_connect('localhost','root','','bookspyramid');
$sql="SELECT audiono FROM bookinfo where bookname like '%$searchword%'";
if($result=mysqli_query($con,$sql))
{
  if(mysqli_num_rows($result)>0)
  {
    while($row=mysqli_fetch_array($result))
    {  
      echo "<a href='..\datafile/viewaudio.php?del=$row[audiono]'>Download AudioBook </a>";
    }
  }
}
</html>

From above code I passed value of audiono to below viewaudio php file, #code3

<html>
<?php
$remitem=$_GET['del'];
$con=mysqli_connect('localhost','root','','bookspyramid');
?>
<audio controls style="width: 90%; margin-left: 5%;">
<source src="data:audio/wav;base64,<?php echo base64_encode($remitem); ?>" type="audio/wav">
</audio>
</html>

If instead of just above code I write below code then file successfully gets downloaded, #code4

<?php
    $remitem=$_GET['del'];
    header('Content-type: application/audio');
    header('Content-Disposition: inline; filename="' . $remitem . '"');
    header('Content-Transfer-Encoding: binary');
    header('Accept-Ranges: bytes');
    @readfile($remitem);
?>

But if I want to show the audio format(online play). In Edge it is displaying 'This type of audio file isn't supported'

In output of #code3, audiofile is showing but it is not working(gray in colour). I want it to work.

Utpal Gaurav
  • 63
  • 1
  • 10
  • 1
    What's the purpose of `base64_encode` there? And why not mention what your input variable contains? – mario May 12 '19 at 16:26
  • what's a sample value of $remitem, and does the file actually *exist* on your server? – Franz Gleichmann May 12 '19 at 16:26
  • @FranzGleichmann, $remitem exists in server and it's value is showing 1557673700.wav (all correct) – Utpal Gaurav May 12 '19 at 16:29
  • @mario, I copied that line from an answer in stackoverflow (as I mentioned in question), I don't know the use. Please help! – Utpal Gaurav May 12 '19 at 16:30
  • Why are you connecting to a database if you're not using that database? What is the input on `$_GET['del']`? Why do you expect that input to be a binary file? – David May 12 '19 at 16:32
  • 1
    No, you did not mention that you copied it from another answer. Much less from which answer. ("Please help!" is frankly annoying.) – mario May 12 '19 at 16:33
  • @David, I am sucessfull to download the audiofile using codes (so it is a binary file). But not able to display audiofile in php page. – Utpal Gaurav May 12 '19 at 16:36
  • 1
    @UtpalGaurav: That only makes the overall question less clear. Please elaborate on the specifics of what your values are, what output you're getting, what the problem is, etc. This is an opportunity to practice some debugging and dig deeper than "it doesn't work". What is the exact HTML in the Page Source in your browser? What do you expect that HTML to be? What is the request made in the browser's debugging tools network tab for the audio file? What is the server's response to that request? What is the value you're base-64 encoding? Why are you encoding it? – David May 12 '19 at 16:37

1 Answers1

1

I have checked that echo $remitem; prints exact same name of audiofile with extension that i want to play.

Then why are you base-64 encoding it? The src expects a URL (or base-64 encoded binary data, but you don't have binary data, you have a file name) so just use the name of the file:

<source src="<?php echo htmlspecialchars($remitem); ?>" type="audio/wav">

Note the use of htmlspecialchars() here. This is because you are directly outputting user input to the page. This is a potential security hole and you need to ensure users are not crafting links to your page which would cause your code to compromise other users on your site. Much more information on the subject can be found here.

Once you're linking to the file name as a URL, the next question becomes... Is this the right URL? Well, that's up to you. You have the name of a file, but where is the actual file? That location on the file system would need to translate to a URL on the web server. It's up to you to determine what that URL is for that file. Once you have that, you can make any modifications needed (such as prefixing folders to the file name in the URL, making a relative or absolute URL, etc.) to turn the file name into a valid URL to retrieve the file.

Community
  • 1
  • 1
David
  • 176,566
  • 33
  • 178
  • 245