symfony2 存储上传的文件非 rootweb [英] symfony2 store uploaded file non rootweb
问题描述
我上传文件时遇到问题,我将上传的文件保存在非公共文件夹(非 webroot 文件夹)中,然后我想获取所有上传文件的 webPAth 以在视频库中使用它们.
I have problem with uploading files, I'm saving uploaded files in a non public folder (non webroot folder), and then I want to get the webPAth of all uploaded files to use them in a video gallery.
$videos = $repository->getVideosThumbs($this->getUser());
然后在我的树枝上
{% for video in videos %}
<a href ="{{ path('neoctus_videobundle_default_watchvideo', {'id' : video.id, 'salt' : app.user.salt }) }}" >
<img id="{{ video.id }}" src="/uploads/images/{{video.imgPath}}" alt="1st image description" height="150" width="150" />
</a>
{% endfor %}
但是我无法访问图片,因为该文件夹不是公开的,我知道我必须通过 x-sendFile 但我不知道如何在标题中发送多个 jpeg 文件,然后如何在树枝后恢复.
but I can not access the pictures because the folder is not public, I know I have to go through x-sendFile but I do not see how I can send multiple jpeg files in the header and then how recover after the twig.
推荐答案
您可以创建控制器操作来访问存储在非公共文件夹中的文件.在该操作中,您打开文件并流式传输到浏览器.
You could create a controller action to access files that are stored in a non public folder. In that action, you open file and stream in to browser.
参见 http://php.net/manual/en/function.readfile 中的示例.php
你需要改变
header('Content-Disposition: attachment; filename='.basename($file));
到
header('Content-Disposition: inline; filename='.basename($file));
更新:
当您拥有可以流式传输请求文件的控制器操作时,您可以通过使用所需的文件标识符请求该操作来在您的 TWIG 中呈现它:
When you have your controller action that would stream requested file, you can render it in your TWIG by requesting that action with required file identifier:
<img src="{{ path('route_to_stream_action', {'fileId':'some_id'}) }}">
浏览器会像对待直接访问一样对待流式文件,因此您可以对其应用任何 CSS.
Browser will treat streamed file the same way as if it was accessed directly, so you can apply any CSS to it.
更新:
示例控制器操作:
public function streamFileAction($fileId)
{
// implement your own logic to retrieve file using $fileId
$file = $this->getFile($fileId);
$filename = basename($file);
$response = new StreamedResponse();
$response->setCallback(function () use ($file){
$handle = fopen($file->getRealPath(), 'rb');
while (!feof($handle)) {
$buffer = fread($handle, 1024);
echo $buffer;
flush();
}
fclose($handle);
});
$d = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, $filename);
$response->headers->set('Content-Disposition', $d);
$response->headers->set('Content-Type', $file->getMimeType());
return $response;
}
这篇关于symfony2 存储上传的文件非 rootweb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!