duadpnld426905 2013-07-18 20:21
浏览 42
已采纳

如何在symfony2中跨视图共享文件

I'm building a multi step file upload using symfony2.

The first screen allows users to upload a CSV file.

The second screen allows users to map a database column to a CSV column.

I have two methods in my controller, one for each screen.

file_upload => First screen

match_csv => Second Screen

Inside the file_upload method, I have the following code:

 return $this->redirect($this->generateUrl('match_csv', 
                        array(
                               'accountId' => $accountId,
                               'projectId' => $projectId,
                               'file' => base64_encode( $file->getPathname() )
                              )
                         ), 301);

and in my match_csv method, I have the following code:

    $file = base64_decode($file);
    $csvFile = new \SplFileObject( $file );
    $csvFile->setFlags(\SplFileObject::READ_CSV);

My problem is:

When I switch views from file_upload to match_csv, the server returns an error stating that the file no longer exists.

It is of my understanding that PHP deletes temp files as soon as a script's execution stops. Hence, when I switch views in symfony2 the file_upload method stops and the file is deleted.

My questions is/are:

Is there a better alternative to share an uploaded file across views?

Is it a good idea to write a temporary file myself, and then delete it right after I stopped using it so that Symfony2/PHP do not delete it automatically?

  • 写回答

1条回答 默认 最新

  • doukong1901 2013-07-18 20:27
    关注

    It's not a good idea to store the file name in the url, it's a security risk. Use the session for that.

    1. I would do it like this: upload the file to a temporary area and store the path you moved it to in the session.

    2. Then, in your second method, get the path from the session, do the parsing and then remove the file. That's it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集