gpt4 book ai didi

php - Zend Framework 2 文件下载

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:51:51 24 4
gpt4 key购买 nike

我制作了一个表单,使用 Zend\Filter\File\RenameUpload 过滤器将文件上传到文件夹 ./data/uploads

这就像一个魅力。我现在的问题是如何提供这个文件给用户下载呢?

我觉得应该是这样的:

$response->setContent(file_get_contents('./data/uploads/file.png'));

但我想知道最好的方法是什么。

最佳答案

对于任何进入此线程寻找答案的人来说,这是一个有效的解决方案,而且它使用的是流!

public function downloadAction() {
$fileName = 'somefile';

$response = new \Zend\Http\Response\Stream();
$response->setStream(fopen($fileName, 'r'));
$response->setStatusCode(200);

$headers = new \Zend\Http\Headers();
$headers->addHeaderLine('Content-Type', 'whatever your content type is')
->addHeaderLine('Content-Disposition', 'attachment; filename="' . $fileName . '"')
->addHeaderLine('Content-Length', filesize($fileName));

$response->setHeaders($headers);
return $response;
}

在这里找到: force download using zf2

更多细节在这里: sending stream responses with zend

关于php - Zend Framework 2 文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16092493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com