我有以下功能强制下载文件:

static public function download($file, $options=array()) {
    $content = (isset($options['content'])) ? $options['content'] : '';
    $contentType = (isset($options['contentType'])) ? $options['contentType'] : '';
    header('Cache-Control: public');
    header('Content-Description: File Transfer');
    header('Content-Disposition: attachment; filename='.File::filename($file));
    header('Content-Type: '.$contentType);
    header('Content-Transfer-Encoding: binary');
    if ($content!='') {
        echo $content;
    } else {
        readfile($file);
    }
}

我发送PDF文件和contentType =“ application/pdf”。问题是,当我尝试打开下载的PDF文件时,它说“打开该文档的错误。文件可能损坏”。很奇怪,因为我可以打开原始文件,它们看起来完全相同(文件名,大小等)

有帮助吗?

解决方案

确保在运行此功能之前没有输出 exit 在此功能结束时构造:)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top