我正在研究一种通过 PHP 提供 MP3 文件的方法,在获得如此巨大的帮助后,我让它工作了 here
但是,当我将它用作音频标签中的源时,该示例似乎不起作用
<html>
<head>
<title>Audio Tag Experiment</title>
</head>
<body>
<audio id='audio-element' src="music/mp3.php" autoplay controls>
Your browser does not support the audio element.
</audio>
</body>
</html>
这是 PHP
<?php
$track = "lilly.mp3";
if(file_exists($track))
{
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-length: ' . filesize($track));
header('Content-Disposition: filename="lilly.mp3"');
header('X-Pad: avoid browser bug');
Header('Cache-Control: no-cache');
readfile($track);
}else{
echo "no file";
}
所以我在想(这可能是一个非常糟糕的主意,你告诉我)当有人请求 .MP3 时,我可以设置 Apache 来提供 PHP 文件。
所以我有三个问题
- 这行吗
- 好主意/坏主意?
- 我需要做什么?将“AddType application/x-httpd-php .mp3”放入 httpd conf 是否可行?
最佳答案
您的代码中存在一些错误:
- 一个资源只能有一个Content-Type值(value)。因此,您必须决定要使用的媒体类型。我建议
audio/mpeg
。 - 您忘记在 Content-Disposition 中指定配置.如果您只想提供文件名而不想更改配置,请使用默认值
inline
。
其余看起来不错。但如果找不到文件,我也会发送 404 状态码。
$track = "lilly.mp3";
if (file_exists($track)) {
header("Content-Type: audio/mpeg");
header('Content-Length: ' . filesize($track));
header('Content-Disposition: inline; filename="lilly.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($track);
exit;
} else {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
echo "no file";
}
关于php - 请求 MP3 文件时设置 apache 为 PHP 提供服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1516779/