Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Need to fix a bug for streaming a private MP3 file from PHP

I have a collection of private music files that I don't want people hard linking to, so I created an access token-based system to allow a one-use scenario that has to be generated by a human upon request. This just ensures that the music being played doesn't get linked. I am having a problem with making the MP3 file play with PHP; it works perfectly fine from a hard linked MP3.

I replicated the headers for a plain MP3 file for the PHP script that is supposed to play the private files, but about halfway through the song, it is either detected as an invalid file, or the browser somehow doesn't have access to the rest of the song.

Are the headers below that I am using correct for streaming one MP3 file using readfile() through PHP? If not, how should I go about correcting this script? I have tried most of the other questions' answers on stack overflow, but I have had no success.

These are the headers for the MP3 file and the PHP script:

HTTP/1.1 200 OK
Date: Fri, 03 Aug 2012 19:23:06 GMT
Server: Apache/2.2.22 (Ubuntu)
Last-Modified: Sat, 28 Jul 2012 23:45:21 GMT
ETag: "b7b58-7cd356-4c5ec6bf4c640"
Accept-Ranges: bytes
Content-Length: 8180566
Content-Type: audio/mpeg

This is the script that I wrote:

<?php
    $file = "/var/private/music/musicfile.mp3";
    header_remove();
    header("Last-Modified: Sat, 28 Jul 2012 23:45:21 GMT");
    header("ETag: \"b7b58-7cd356-4c5ec6bf4c640\"");
    header("Accept-Ranges: bytes");
    header("Content-Length: ".filesize($file));
    header("Content-Type: audio/mpeg");
    readfile($file);
?>

What am I doing wrong?

Comments