来自php标头的可下载的Mp3文件无法正常工作

Don*_*uma 2 html php mp3 header

你好,所以这是交易我在我的服务器上有我的mp3文件,每个都在自己的文件夹中.在该文件夹中是mp3和带有以下脚本的php文件:

<?php
// We'll be outputting a PDF
header('Content-type: audio/mp3');

// It will be called file.mp3
header('Content-Disposition: attachment; filename="mysong.mp3"');
// The PDF source is in original.mp3
readfile("mysong.mp3");
?>
Run Code Online (Sandbox Code Playgroud)

问题是,当我点击进入那个php页面时,标题是为了让它自动下载mp3文件,当它下载时它会下载一个300KB的文件但是当我去mp3文件的实际链接时它会播放它完美地在浏览器中所以我猜测给出标题的php文件有问题.

Fos*_*sco 6

试试这个来自http://us.php.net/readfile的更完整的例子

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>
Run Code Online (Sandbox Code Playgroud)