Symfony3 - 处理文件上传/编辑的好方法是什么?

时间:2016-03-03 15:33:22

标签: forms file upload symfony

Cookbook解释代码与NewAction()控制器配合得很好,但如何编辑具有此技术文件的表单?

这个编辑控制器总是擦除文件,即使它不存在。这只是一个问题,当我只想编辑文本而不是文件本身...

public function editAction(Request $request, Model $model)
{
    $deleteForm = $this->createDeleteForm($model);
    $editForm = $this->createForm(ModelType::class, $model);
    $editForm->handleRequest($request);

    if ($editForm->isSubmitted() && $editForm->isValid()) {

            $file = $model->getPicture();
            $fileName = substr(md5(uniqid()), 0, 10) . '.' . $file->guessExtension();

            $uploadDir = $this->container->getParameter('kernel.root_dir') . '/../web/' . Model::UPLOAD_DIR;
            $file->move($uploadDir, $fileName);

            $model->setPicture($fileName);

        $em = $this->getDoctrine()->getManager();
        $em->persist($model);
        $em->flush();

        return $this->redirectToRoute('my_route', array('id' => $model->getId()));
    }

0 个答案:

没有答案