php - 带标题下载的文件不起作用

标签 php http audio http-headers ogg

我正在尝试下载具有动态网址的音频ogg文件

function getAudio($fileName){

    $localFile = __DIR__ . '/../../../shared/call_review/' . $fileName;
    $fp = fopen($localFile, 'r');
    $fp or die('Could not open file "' . $fileName . '"' . PHP_EOL);

    $contents = fread($fp, filesize($localFile));
    $contents or die('could not read file');

    header('Content-Description: File Transfer');
    header('Cache-Control: public');
    header('Content-Type: audio/ogg');
    header("Content-Transfer-Encoding: binary");
    header('Content-Length: '.filesize($contents));
    header("Content-Type: audio/ogg");
    header("Content-Disposition: attachment; filename=" . $fileName);

   echo $contents;
   exit();
}

但是保存文件后,它不起作用或损坏。

最佳答案

问题似乎是这一行:

header('Content-Length: '.filesize($contents));

文件大小应来自filesize($localFile),如前面几行所示,
所以应该是:
header('Content-Length: '.filesize($localFile));

或者只使用$contents变量的长度:
header('Content-Length: '.strlen($contents));

关于php - 带标题下载的文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51325200/

相关文章:

android - ExoPlayer 背景音频和前景视频?

java - 如何暂停播放并停止来自parse.com(android)的音频文件流?

php - wordpress自定义联系表单问题

javascript - 如何在 FullCalendar 中为事件调用自定义属性或字段?

php发送邮件并在同一页面显示成功信息

java - 如何在java中的http post中设置用户名?

rest - 如何保护 REST GET 方法的安全?

android - 从 DB Android 播放媒体

php - Where 子句中的数组

java - 无论如何从 URL 中删除参数