PHP returns an MP3 file

Asked

Viewed 171 times

0

I have a small music application where files are loaded from a URL like "http://www.mymusicapp.com/get_song.php?name=SOME_MUSIC".

Locally the application works very well.

But when I put it on the server my application generates an error in the browser console.

Uncaught Invalidstateerror: Uncaught Invalidstateerror: An Attempt was made to use an Object that is not, or is no longer, usable.

My PHP code:

 $song_name = $_GET['name'];
 $song_file = "{$_SERVER['DOCUMENT_ROOT']}/assets/musics/{$song_name}.mp3";

 if( file_exists( $song_file ) )
 {
    header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
    header('Content-Length: ' . filesize($song_file));
    header('Content-Disposition: attachment; filename="'.$song_name.'.mp3"');
    header("Content-Transfer-Encoding: binary");      
    header('X-Pad: avoid browser bug');        
    readfile( $song_file );
  } 
  • 1

    Your code is not at all safe Bruno, it is possible an attack of code Injection very easily. http://www.mymusicapp.com/get_song.php?name=" echo "code injection é muito massa!"; $var = "

  • Thank you for the gmsantos remark! But I would like to make him take it first of all. You would know how?

  • The problem is not PHP, PHP does not send errors to the browser console (at least natively), much less errors of this nature. Your problem is in the Javascript code which is responsible for handling and playing the MP3. Your question in its current form is not possible to be answered exactly. Anyway, it looks to me like you’re using the object before it’s been loaded...

1 answer

1

Well, we have two points, the first is, if you can access the file directly from the link that you’re requesting, then if you can reproduce, and if you’re calling your file by a js, check what’s going on with your ajax, or even your frame, that you’re changing. As I don’t know your environment, I suspect that the problem is in the configuration of your server or your. htacess, where your header might be being ignored.

  • I can access them directly... I realized that in my AJAX request it only loads 2.1 MB of songs...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.