2011-08-09 59 views

回答

17

我终于找到了解决办法

的文档是错误的

而不是

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

它应该是:

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

使用的Symfony 2.7(内Silex的),我currentlly发现 - >的getData()返回的文件名的只是字符串,所以 - > move()等不起作用。 –

+2

啊,我有一个硬编码

标签,并没有添加enctype =“multipart/form-data”属性给它;添加,返回我UploadedFile对象,而不是一个字符串! –

18

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

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