Link to home
Start Free TrialLog in
Avatar of aej1973
aej1973

asked on

open a blob file php/mysql...

Hi, I have a BLOB file in my data base which is a voice file. From phpmyadmin if I click on this file I can open it and hear the contents. How do I open this file and listen to it using php? I have attached the code I am using to try and accomplish this. Thank you.

A
voicemail.php
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Tell us a little more about the "file" which is really a column in a data base, right?  Where does it come from, etc?
Avatar of aej1973
aej1973

ASKER

Ray, the voice file is stored on the server at /var/spool/test/voicemails.
OK, that's a start.  Where does it come from, etc? Tell us the format of the file.  What happens when you create a link to such a file and click the link?  Can the file play in HTML5?  Does it require a browser plug-in?  Anything more you can share with us will be most helpful!
Avatar of aej1973

ASKER

When I click on the link on the php file I see gibberish as follows:

RIFF¤WAVEfmt @€>data€ðÿèÿàÿàÿðÿøÿðÿèÿàÿàÿèÿèÿðÿøÿøÿðÿèÿèÿèÿèÿðÿèÿàÿØÿØÿØÿØÿØÿØÿàÿèÿØÿÀÿ°ÿ¸ÿÀÿÀÿÐÿàÿèÿðÿøÿøÿøÿøÿøÿðÿèÿèÿðÿèÿèÿèÿðÿðÿðÿøÿøÿøÿðÿèÿðÿðÿ øÿðÿðÿèÿÐÿÈÿÈÿÀÿÈÿØÿàÿèÿøÿøÿ (80(( ðÿàÿèÿèÿèÿèÿèÿèÿðÿøÿøÿøÿðÿ ðÿðÿèÿÐÿÐÿàÿèÿèÿøÿøÿðÿøÿøÿøÿøÿ  øÿøÿðÿèÿðÿøÿøÿèÿàÿØÿØÿàÿèÿøÿøÿøÿøÿøÿøÿ (088(øÿøÿ øÿðÿðÿðÿàÿØÿøÿøÿèÿèÿèÿàÿØÿØÿàÿèÿèÿøÿøÿðÿèÿèÿØÿàÿðÿøÿðÿøÿøÿèÿP8àÿ`0@0èÿ8øÿðÿøÿðÿèÿØÿØÿÈÿÀÿ¸ÿ¸ÿ¸ÿ¸ÿ¸ÿÈÿÐÿØÿàÿèÿèÿàÿØÿàÿàÿàÿðÿøÿðÿøÿ  (øÿèÿøÿøÿðÿðÿðÿðÿøÿøÿðÿèÿðÿðÿèÿèÿðÿðÿøÿ ( 0(øÿðÿðÿøÿøÿðÿ

The file is a .bin file.
ASKER CERTIFIED SOLUTION
Avatar of rinfo
rinfo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let me try this one more time: Where does this file come from?
Avatar of aej1973

ASKER

Ray, attached is a jpeg image of how the file looks in the DB. This voice file is a .wav file and when i click on the recording link I can play the file.

A
vm.JPG
What happens if your PHP script simply uses echo to write the file to the browser?  No other browser output at all - just the echo.  If that doesn't work, you might copy the file into the file system and send a link to the file into the browser output stream using something like this:
<bgsound src="path/to/wav/file.wav" />

Most browsers will play a bgsound tag.  An alternative is recommended here:
http://www.w3.org/wiki/HTML/Elements/bgsound
Avatar of aej1973

ASKER

I noticed something interesting. I copied this voicemail table and dumped it on a windows machine to see if I can play the file when I click on the link, and I could. So it looks to me as if the file is located in my database and not in the "var/spool/asterisk/voicemail/myMails/203/INBOX" location. Any thoughts? Thank you.
Avatar of aej1973

ASKER

What I was able to do was to run the following query and dump the file in the /var directory;

select recording from voicemessages where uniqueid='1' into DUMPFILE 'tmp/voicemail_recording.wav';

and I was able to retrieve the file from the /tmp directory. Now the question I have is what happens if I have more than one file that I dump? When I tested this even though the query result showed me two rows were dumped I see only one file since all the files have same name (ie) voicemail_recording.wav. What can I do in this situation? Thank you.
Why dont you try what i have suggested.
Avatar of aej1973

ASKER

Will do.
Your bloa data is nothing but a wav file in raw binary data format  you can write to a file.
First create a temp file using fopen
Write blob data into this file using fwrite. Fwrite(tenpfilehandle,row[blobfield])
Once temp file is written you can play as mentioned earlier .
And dont forget as you are dealing with binary files you have to add  b to file modes. rb to open and wb to write
Avatar of aej1973

ASKER

rinfo, my code is as follows:

while($row = mysql_fetch_assoc($result))
{
        echo '<tr bordercolor=/"#660066/">';
            echo '<td>'. $row['origtime'].'</td>';
            echo '<td>' . $row['callerid'].'</td>';
            echo '<td>'. $row['duration'].'</td>';
            $data=$row['recording'];
            $wavFileName = "/tmp/voicemail_recording.wav";
            $fh = fopen($wavFileName, 'w') or die("can't open file");
            fwrite($fh, $data);
            fclose($fh);
            echo '<td>';
            echo '<audio controls="controls"> <source src="wavFileName" type="audio/wav">';
            echo '</audio>';
            echo '</td>';
            echo '<td>'. $test.'</td>';
       echo '</tr>';

}

I have attached a jpg of what I see on the screen. I am not able to play the file. On my Chrome browser I am able to see the image of the audio control box, but on my FF and IE browser the controls do not show up. What will I need to change? Thank you for your help.

A
voice.jpg
Wavefilenane in src woukd be name of file you have created with the path
Avatar of aej1973

ASKER

you mean it is has to be "/tmp/voicemail_recording.wav" ?
Yes
Avatar of aej1973

ASKER

Changed the code to the following and I still have the same issue:

while($row = mysql_fetch_assoc($result))
{
        echo '<tr bordercolor=/"#660066/">';
            echo '<td>'. $row['origtime'].'</td>';
            echo '<td>' . $row['callerid'].'</td>';
            echo '<td>'. $row['duration'].'</td>';
            $data=$row['recording'];
            $wavFileName = "/tmp/voicemail_recording.wav";
            $fh = fopen($wavFileName, 'wb') or die("can't open file");
            fwrite($fh, $data);
            fclose($fh);
            echo '<td>';
            echo '<audio controls="controls"> <source src="/tmp/voicemail_recording.wav"   type="audio/wav">';
            echo '</audio>';
            echo '</td>';
            echo '<td>'. $test.'</td>';
       echo '</tr>';

}
Avatar of aej1973

ASKER

ok, looks like I have made some good progress, I am able to view the voice control box on my webpage and and am able to listen to the messages. The only problem is, even though there are several rows of messages in the db only the last message is being played, all other records picked up are unique to the row. My code is as follows;


/********************************************
         $query = mysql_query("SELECT recording FROM voicemessages");

  while ($row = mysql_fetch_assoc($query)){

            $data=$row['recording'];
            $wavFileName = "voicemail_recording.wav";
            $fh = fopen($wavFileName, 'wb') or die("can't open file");
            fwrite($fh, $data);
            fclose($fh);

    echo '<audio controls="controls">';
    echo '<source src="voicemail_recording.wav" type="audio/wav">';
    echo 'Your browser does not support this audio format.';
    echo '</audio>';
    echo "<br>";
    echo "<br>";

}

********************************************/

Thanks for the help...
voice.jpg
Thats because you are assigning same wav filename to all records .make filename unique for all record.like recording_$id_file.wav.
Avatar of aej1973

ASKER

Rinfo, thank you very much. It was a interesting module to work on, thanks for all the help.

A