我创建了一个文件上传页面。在我的控制器中,我想获取视图的上传路径,并将其添加到数据库中以获取特定ID。为此,我希望文件的路径将其发送到存储库。我在控制器中使用时的问题
if ($request->getMethod() == 'POST')
{
$form->bind($request);
$file = $form["file"]->getData();
/* here it is giving the path like /tmp/phpkR4kgD */
$em = $this->getDoctrine()->getManager();
$user->upload();
}
this is my entity
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
public function upload()
{
if (null === $this->file)
{
return;
}
$this->file->move($this->getUploadRootDir(), $this->file->getClientOriginalName());
$this->path = $this->file->getClientOriginalName();
$this->file = null;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir() . DIRECTORY_SEPARATOR . $this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir() . DIRECTORY_SEPARATOR . $this->path;
}
protected function getUploadRootDir()
{
return __DIR__ . '/../../../../web/'. $this->getUploadDir();
}
protected function getUploadDir()
{
return 'uploads/';
}
我在symfony
的网络文件夹中创建了我的上传文件夹答案 0 :(得分:1)
从它调用upload()方法时,将临时路径带到实体。
在实体中,它将获得原始路径$ this-> path = $ this-> file-> getClientOriginalName();
所以使用return语句从那里返回控制器的原始路径,你可以将它保存在数据库中......