【问题标题】:symfony2 file uploadsymfony2 文件上传
【发布时间】:2011-08-09 15:26:43
【问题描述】:

我已遵循本指南:http://symfony.com/doc/current/reference/forms/types/file.html

并在此处测试样本

但是,当我尝试代码时,我有一个错误:

Call to undefined method Symfony\Component\Form\Form::move()

这行发生在:

$form['attachment']->move($dir, $someNewFilename);

不知道为什么会出现这个错误?

【问题讨论】:

    标签: upload symfony


    【解决方案1】:

    这不使用“表单”类,但我已经成功地直接从请求中检索上传:

    /* @var Request */
    $request = $this->getRequest();
    
    /* @var UploadedFile */
    $uploadedFile = $request->files->get('upfile'); //upfile must be the value of the name attribute in the <input> tag
    if (null === $uploadedFile)
        return new RedirectResponse($this->generateUrl('_upload_index'));
    
    /* @var string*/
    $filename = $uploadedFile->getPathname();
    

    【讨论】:

      【解决方案2】:

      我终于找到了解决办法

      文档有误

      而不是:

      $form['attachment']->move($dir, $someNewFilename);
      

      应该是:

      $form['attachment']->getData()->move($dir, $someNewFilename);
      

      【讨论】:

      • 使用 Symfony 2.7(在 Silex 中),我目前发现 ->getData() 只返回文件名的字符串,所以 ->move() 等不能处理它。
      • 啊,我有一个硬编码的
        标签,没有添加 enctype="multipart/form-data" 属性;添加它会返回 UploadedFile 对象而不是字符串!
      【解决方案3】:

      现在最好按照官方文档中的说明进行操作: http://symfony.com/doc/2.0/cookbook/doctrine/file_uploads.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-15
        • 2015-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多