我正在阅读并尝试应用这篇关于 File Uploads的文章,但是有一个问题。据说这是:
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$document->upload();
$em->persist($document);
$em->flush();
$this->redirect(...);
}
应该进入控制器,这里是upload
函数的定义
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->file) {
return;
}
// we use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and then the target filename to move to
$this->file->move($this->getUploadRootDir(), $this->file->getClientOriginalName());
// set the path property to the filename where you'ved saved the file
$this->path = $this->file->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->file = null;
}
但是我应该把这个放在哪里?我在控制器中尝试了调用它的操作并发生错误 -Fatal error: Call to undefined method...
我还尝试将它放在不同的类和文件中,并使用 use 添加其命名空间,但错误仍然存在。你能告诉我错误在哪里吗?:)