[英]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.