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

时间:2018-07-13 12:32:40

标签: 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();
}

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

1 个答案:

答案 0 :(得分:0)

问题似乎是这一行:

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

文件大小应来自filesize($localFile),如前几行所示, 所以应该是:

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

或仅使用$contents变量的长度:

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