繁体   English   中英

PHP 返回 mp3 文件

[英]PHP return mp3 file

我有一个小音乐应用程序,其中的文件是从“ http://www.mymusicapp.com/get_song.php?name=SOME_MUSIC ”之类的 URL 加载的。 该应用程序在本地运行良好! 但是当我把它放在服务器上时,我的应用程序在浏览器控制台中生成了一个错误。

浏览器错误Uncaught InvalidStateError: 尝试使用不可用或不再可用的对象

我的PHP代码是:

 $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 );    

  }     

也许这有效:

$filename = $_GET['name'];
$real_path = "{$_SERVER['DOCUMENT_ROOT']}/assets/musics/{$filename}.mp3";
$mime_type = "audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3";
if(file_exists($real_path)) {
    header('Content-type: {$mime_type}');
    header('Content-length: ' . filesize($real_path));
    header('Content-Disposition: filename="' . $filename . '.mp3"');
    header('X-Pad: avoid browser bug');
    header('Cache-Control: no-cache');
    readfile($real_path);
} else {
    header("HTTP/1.0 404 Not Found");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM