0

I've got an embedded mp3 player that i'm running in a webpage. When i load a physical mp3's path as the file url, it works just fine, but when i try to load mp3 binary data from a php endpoint, it fails.

here's what I'd doing to output the mp3 data via php:

//Begin writing headers
header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Cache-Control: public' );
header( 'Content-Description: File Transfer' );
header( 'Content-Type:audio/mpeg');
header( 'Content-Disposition: inline;');
header( 'filename=SoundFile.mp3' );
header( 'Content-Transfer-Encoding: binary' );

//supply the length
$len = strlen($res['ReturnValue']);
header( 'Content-Length: ' . $len );

echo $res['ReturnValue']; exit();
// $res['ReturnValue'] is the actual binary data of the mp3

Also, when I run that php code in the browser, it causes a file download of the correct name and filesize, but the file itself wont play on anything (itunes / winamp / etc) as if it is corrupt.

What am I doing wrong with the output of this data that is causing it to not play in the embedded mp3 player?

Kristian
  • 21,204
  • 19
  • 101
  • 176
  • What browsers have you tried? – Paul Dessert May 22 '12 at 21:38
  • any browser fails. it saves a file, and the file is just corrupted somehow. it wont play in a media player -- the file outputted isn't any good. – Kristian May 22 '12 at 21:39
  • `header( 'filename=' . 'SoundFile' . '.' . 'mp3' );`? Pretty sure that should be part of the previous call to `header()`... – DaveRandom May 22 '12 at 21:50
  • Also, how did you assign a value to `$res['ReturnValue']`? That is the crux of the problem... – DaveRandom May 22 '12 at 21:51
  • Not sure, but doesn't `header( 'Content-Description: File Transfer' ); ` force a download? – Paul Dessert May 22 '12 at 21:51
  • $res['ReturnValue'] is the binary data returned from an appserver call (.net) – Kristian May 22 '12 at 21:53
  • @Paul Actually that is a non-standard, more or less meaningless header. The important thing is `Content-Disposition: inline;` which should actually *not* force a download, but most browsers will anyway because they have no native handler for the `audio/mpeg` type – DaveRandom May 22 '12 at 21:54
  • @Kristian Well chances are it is that data which is corrupt. Do you have a URL you can post where I can get some content and have a look at it? – DaveRandom May 22 '12 at 21:55
  • @DaveRandom I definitely have data -- I can print it out and see that it is there... its obviously a bunch of jibberish to look at – Kristian May 22 '12 at 21:56
  • @Kristian I'm sure you do have data, I just suspect it is not *valid* data. It will be gibberish to view as ASCII, do you get a valid MP3 if you write it to a file? You can `file_put_contents('test.mp3', $res['ReturnValue']);` and try and play the `test.mp3` file that is created. Failing that, is there any way you can post a Hex dump of the binary data? – DaveRandom May 22 '12 at 21:57
  • @DaveRandom I actually just asked our app server dev about it... and he definitely forgot to encode the data as mp3. HA. – Kristian May 22 '12 at 22:04
  • 1
    @Kristian lol. Hopefully that should solve your problem. Never rely on other people. As a side note, please remove the `Content-Description` header (non-standard, meaningless) and the `Content-Transfer-Encoding` header (meaningless in the context of HTTP), merge `Content-Disposition` into one line (incorrect usage of PHP's `header()` function) and change the very last line to `exit($res['ReturnValue']);` (shorter and more readable). Also when outputting binary data be very careful about data outside `` tags, [BOM](http://en.wikipedia.org/wiki/Byte_order_mark)s especially. – DaveRandom May 22 '12 at 22:09

1 Answers1

0

The byte data was not being correctly output to me, thus, my headers were not the problem.

Kristian
  • 21,204
  • 19
  • 101
  • 176