【问题标题】:symfony2 store uploaded file non rootwebsymfony2 存储上传的文件非 rootweb
【发布时间】:2013-09-23 17:13:31
【问题描述】:

我在上传文件时遇到问题,我将上传的文件保存在非公共文件夹(非 webroot 文件夹)中,然后我想获取所有上传文件的 webPAth 以在视频库中使用它们。

$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 文件,然后如何在树枝后恢复。

【问题讨论】:

    标签: symfony upload twig x-sendfile


    【解决方案1】:

    您也可以在 twig 中使用“控制器”功能:

    {{ render(controller('AcmeBundle:Controller:action')) }}
    

    即将推出

    AcmeBundle 中ControllerController 中的actionAction。

    希望这能有所帮助。

    【讨论】:

      【解决方案2】:

      您可以创建一个控制器操作来访问存储在非公共文件夹中的文件。在该操作中,您打开文件并流式传输到浏览器。

      参见http://php.net/manual/en/function.readfile.php的示例

      你需要改变

      header('Content-Disposition: attachment; filename='.basename($file));
      

      header('Content-Disposition: inline; filename='.basename($file));
      

      更新:

      当您的控制器操作可以流式传输请求的文件时,您可以通过请求具有所需文件标识符的操作在 TWIG 中呈现它:

      <img src="{{ path('route_to_stream_action', {'fileId':'some_id'}) }}">
      

      浏览器会将流式文件视为直接访问,因此您可以对其应用任何 CSS。

      更新:

      示例控制器操作:

      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;
      }
      

      【讨论】:

      • 感谢您的回答。但我知道如何直接在浏览器上读取文件,我需要的是如何将这些文件发送到我的树枝,以便我可以在它们上应用一些 css .. 有什么想法吗?!
      • 我的回答对你有帮助吗?
      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-17
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      相关资源
      最近更新 更多