Skip to content
Advertisement

PHP Download MP3 files from directory on server

I am attempting to download MP3 files to the user computer located in a directory named “songs” on the server. I have been able to run a script which downloads these files through the browser. However, these files download strictly as text with .mp3 extension. I want these files to be playable mp3 files upon downloading from the server.

Here is my PHP script.

JavaScript

//Script currently downloads a list of all songs found on server

JavaScript

}

?>

Here is an example of the result I get in a txt file.

Established Database Connection
01 In 1983 He Loved To Fly.mp3′>01 In 1983 He Loved To Fly.mp3

Advertisement

Answer

First, header()should be sent before any output which includes echo, print_r, any html, a blank space before the opening tag(eg. <?php). Refer to the manual

Second, if you want to response with the content of a file to the browser, your script should not output anything else. Any output besides the content will be considered a part of the content. Unless you send it as multipart and your client is able to handle it.

So, an example

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement