在 Symfony 3.4 中上传文件 - 更新操作 - 我应该如何编程编辑操作?



我的editAction((有问题,看起来问题出在路径上: "文件 "G:\xampp5.6\htdocs\future\future/web/uploads/images/G:\xampp5.6\tmp\phpB0E6.tmp" 不存在"。
我不知道我能做什么。

/**
* Displays a form to edit an existing blog entity.
*
* @Route("/{id}/edit", name="blog_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, Blog $blog)
{
$blog->setImage(
new File($this->getParameter('image_directory').'/'.$blog->getImage()));
$deleteForm = $this->createDeleteForm($blog);
$editForm = $this->createForm('AppBundleFormBlogType', $blog);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('blog_edit', array('id' => $blog->getId()));
}
return $this->render('blog/edit.html.twig', array(
'blog' => $blog,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}

编辑: 如何设置正确的路径? 因为在我的数据库中路径是 G:\xampp5.6\tmp\phpA7BF.tmp并且应该只命名图像,例如 fa7bcdd50522b0592c5f98ab8313041.jpeg

解决方案:

在 config.yml 中,我们必须使用反斜杠:

parameters:
locale: en
image_directory: '%kernel.project_dir%webuploadsimages'

博客康托勒.php

/**
* Displays a form to edit an existing blog entity.
*
* @Route("/{id}/edit", name="blog_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, Blog $blog)
{

$deleteForm = $this->createDeleteForm($blog);
$editForm = $this->createForm('AppBundleFormBlogType', $blog);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {

new File($blog->getImage());
$file=$blog->getImage();
$fileName=md5(uniqid()).'.'.$file->guessExtension();

$file->move(
$this->getParameter('image_directory'),$fileName
);

$blog->setImage($fileName);

$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('blog_edit', array('id' => $blog->getId()));
}
return $this->render('blog/edit.html.twig', array(
'blog' => $blog,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}

最新更新