【问题标题】:Downloadable Mp3 Files from php headers not working来自 php 标头的可下载 Mp3 文件不起作用
【发布时间】:2010-10-13 18:47:37
【问题描述】:

你好 好吧,这就是交易 我的服务器上有我的 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");
?>

问题是,当我点击去那个 php 页面时,标题应该是这样的,以便它自动下载 mp3 文件,但当它下载时它会下载一个 300KB 的文件,但是当我转到 mp3 的实际链接时文件它在浏览器中完美播放,所以我猜测给出标题的 php 文件有问题。

【问题讨论】:

标签: php html mp3 header


【解决方案1】:

试试这个取自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;
}
?>

【讨论】:

  • 删除这一行:header('Content-Length: ' .filesize($file));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 2015-08-07
  • 1970-01-01
相关资源
最近更新 更多