Page 1 of 1

[SOLVED] Outputting wav sound file through php

Posted: Sat May 28, 2005 6:02 pm
by kaYak
Hi,

I'm trying to output a .wav file through php. I want to embed the wave file on a page without showing the URL to the file. So I have the embed src as a php file. The php file that I am trying to use to get the wave file and send it to the browser to play contains the following code:

Code: Select all

<?php

$digit = $_GET&#1111;'d'] - 1;

/* initialize a session. */
session_start();

$code = $_SESSION&#1111;'ver_code'];

$sound = $code{$digit};

$file_name = $sound . &quote;.wav&quote;;

/*/
Download a file using fpassthru()
/*/
$fullpath = '/home/rowgoco/public_html/anti-spam/sounds/' . $file_name;
$ext = 'wav';

   function mimeTypes($file) {
       if (!is_file($file) || !is_readable($file)) return false;
       $types = array();
       $fp = fopen($file,&quote;r&quote;);
       while (false != ($line = fgets($fp,4096))) {
           if (!preg_match(&quote;/^\s*(?!#)\s*(\S+)\s+(?=\S)(.+)/&quote;,$line,$match)) continue;
           $tmp = preg_split(&quote;/\s/&quote;,trim($match&#1111;2]));
           foreach($tmp as $type) $types&#1111;strtolower($type)] = $match&#1111;1];
       }
       fclose ($fp);
      
       return $types;
   }

   # &#1111;...]

   # read the mime-types
   $mimes = mimeTypes('/usr/local/apache/current/conf/mime.types');

   # use them ($ext is the extension of your file)
   if (isset($mimes&#1111;$ext])) header(&quote;Content-Type: &quote;.$mimes&#1111;$ext]);
   header(&quote;Content-Length: &quote;.@filesize($fullpath));
   readfile($fullpath); exit;

?>
I have tried many different things and this was my last attempt at it. I got most of this code from a comment on php.net. This hasn't seemed to have worked. Can anyone please help. All it outputs to the browser is funny code.

-Kyle

Posted: Sat May 28, 2005 6:20 pm
by Revan
I don't think that's possible, the Browser would need to have a player embeded into it, some one correct me?

Posted: Sat May 28, 2005 6:34 pm
by kaYak
Revan wrote:I don't think that's possible, the Browser would need to have a player embeded into it, some one correct me?
I have...

Code: Select all

<EMBED SRC="play_sound.php?d=" . $digit . "" HIDDEN="true" AUTOSTART="true">
on another page. Site have wav files that autostart. I know they do. I thought this would work like this.

Posted: Sat May 28, 2005 6:36 pm
by timvw
It's possible... Whatever your browser does with the file..... Depends on how you configured your browser :) (Just as every other type of file..)


I usually use the following snippet for a download script
http://timvw.madoka.be/programming/php/download.txt

It does require you have configured http://www.php.net/mime_content_type

Posted: Sat May 28, 2005 6:56 pm
by kaYak

Code: Select all

Fatal error: Call to undefined function: mime_content_type()
It must not be configured. :( Can I manually write in the content-type? I'm not sure what it is for a .wav file though.

Posted: Sat May 28, 2005 7:22 pm
by timvw
timvw@madoka:~$ grep wav /etc/mime.types
application/x-shockwave-flash swf swfl
audio/x-wav wav


So, "audio/x-wav" should do it ;)

Posted: Sat May 28, 2005 7:31 pm
by kaYak
It works Tim! Thank you so much! :D