gpt4 book ai didi

download - Symfony2.1 服务上传

转载 作者:行者123 更新时间:2023-12-02 08:04:17 24 4
gpt4 key购买 nike

我对使用 Symfony 2.1 提供文件有疑问

public function exampleFileAction(){
$response = new Response();

$d = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'File.xls');

$response->setContent(file_get_contents('../uploads/Bundle/Files/File.xls'));
$response->headers->set('Content-Disposition', $d);
return $response;
}

我上面的代码工作正常,但是这是这样做的实际方法吗?用file_get_contents获取文件好不好?

谢谢tny

最佳答案

编辑 2013 年 3 月:

对于大文件不应遵循此响应,游戏中有一位新玩家:https://github.com/igorw/IgorwFileServeBundle

它支持 SendFile 将文件下载委托(delegate)给 Apache/Ngnix,这要好得多。

编辑 2013 年 5 月:

好消息:核心中现已提供内置解决方案:BinaryFileResponse

<小时/>

旧回复:

有一个名为 StreamedResponse 的新 Response 类,它完全符合您想要做的事情,但内存效率更高,并且内核可以很好地处理。

StreamedResponse represents a streamed HTTP response.
A StreamedResponse uses a callback for its content.
The callback should use the standard PHP functions like echo to stream the response back to the client. The flush() method can also be used if needed.

http://symfony.com/doc/master/components/http_foundation/introduction.html#streaming-a-response

因此,对于大文件,您可以在没有 file_get_content 的情况下完成:

<?php
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

$response = new StreamedResponse();
$response->setCallback(function () {
$name = 'bigfile.pdf';
$fp = fopen($name, 'rb');
fpassthru($fp);
});
$d = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'foo.pdf');
$response->headers->set('Content-Disposition', $d);
$response->send();

关于download - Symfony2.1 服务上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11967872/

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