symfony2文件上传

时间:2011-08-09 15:26:44

标签: upload symfony

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

并在此处测试了样本

然而,当我尝试代码时,我有一个错误:

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

这行正在发生:

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

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

3 个答案:

答案 0 :(得分:18)

这不使用'Form'类,但我已成功直接从请求中检索上传:

/* @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();

答案 1 :(得分:17)

我终于找到了解决方案

文档错误

而不是:

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

它应该是:

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

答案 2 :(得分:5)

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

相关问题