I have an MP3 file and I want to send to to the user's browser. What is the most drupalish way of doing this?

ATM I am trying this one out:

    drupal_set_header('Content-type: audio/mp3');
    drupal_set_header('Content-Type: application/force-download');
    drupal_set_header("content-disposition: attachment; filename=".$q[3].".mp3");
    $fn = "/mnt/files/".$q[3].".vox'";
    print $fn;

I still haven't made it work though - the mp3 file that is downloaded contains another page. So I thought drupal must have some way of doing this, since it seems to do anout everything else.

Comments

Vidarls’s picture

http://drupaldocs.org/api/head/function/file_transfer
This is the 100% drupal way of doing it.
An easier, and less sofisticated method is to use the php header function:
http://no.php.net/manual/en/function.header.php
With the header function, you don't have to provide correct mime type information. Providing mime information can at times be troublesome if you want your code to handle different file types.
Peronally I've never gotten the drupal way to work, cause I did not utilize the upload module, and thus I did not automagically get the mime type for my files. it all ended up as 2 Mb of text in my browsers window.

Your current code will just print the path to your file, not the content of the file, as the content of $fn will be the text string "/mnt/files/filename.vox"

-Olegu
http://www.zbz5.net/

marc.bau’s picture

not sure, but i think you should open the file in binary mode... not sure how PHP will do this - but i know this from coldfusion and perl. if not you may get destroyed files...