我有一个下载 mp3 的网站,当我通过 php(curl) 下载它时,歌曲被下载,但专辑封面、艺术家姓名等歌曲的元数据丢失了。
在服务器上,该文件包含所有数据,但在下载时一切都丢失了。这是我的代码:
if (!empty($path) && $path != null)
{
$ch = curl_init($path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false)
{
echo 'CURL Failed';
exit;
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches))
{
$contentLength = (int) $matches[1];
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false); // required for certain browsers
header('Content-Type: audio/mpeg');
header("Content-Disposition: attachment; filename=\"" . urlencode($song->title) . ".mp3\";");
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . $contentLength);
ob_clean();
flush();
echo $data;
exit;
}