【问题标题】:Can't upload file via Symfony 2.8 form upload无法通过 Symfony 2.8 表单上传上传文件
【发布时间】:2016-04-18 12:40:27
【问题描述】:

我一直在尝试通过默认的 symfony 表单设置文件上传和 Symfony\Component\HttpFoundation\File\UploadedFile。 我的表单非常简单,只有一个输入,文件上传按钮和提交按钮。这是我的控制器:

class DefaultController extends Controller
{
public function uploadAction(Request $request)
{
    $document = new Elements();
    $form = $this->createFormBuilder($document)
        ->add('name')
        ->add('file')
        ->add('save', SubmitType::class, array('label' => 'Create Task'))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $document->upload();

        $em->persist($document);
        $em->flush();

        return $this->redirectToRoute('felice_admin_upload');
    }

    return  $this->render('FeliceAdminBundle:Default:upload.html.twig', array(
        'form' => $form->createView(),
    ));
}
}

我还创建了一个实体,将数据保存到数据库。我正在使用教义。我所做的一切都是手动的: http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html

但唯一的例外是我使用了 yml,而不是注解。毕竟,我在尝试上传文件时出错:

File.php 第 37 行中的 FileNotFoundException: 文件“/tmp/phpFMtBcf”不存在

我做错了什么?

【问题讨论】:

    标签: php forms symfony file-upload doctrine


    【解决方案1】:

    好的,我还没有找到我的问题的答案。我试图在不同的论坛上搜索法语:) 所以我的解决方案在接下来。我在实际处理请求之前手动收集文件数据,然后处理请求,接下来我要做的是复制文件而不是移动文件。那没有得到我描述的错误。因此,为了美观和方便,它应该进行相当大的重构,但效果很好。感谢您的关注。

    class DefaultController extends Controller
    {
    /**
     * @Route("/product/new", name="app_product_new")
     */
    public function newAction(Request $request)
    {
        $product = new Product();
        $form = $this->createFormBuilder(null, array('csrf_protection' => false))
            ->add('pic', FileType::class, array('label' => 'Picture'))
            ->add('Send', 'submit')
            ->getForm();
    
        $pic = $request->files->get("form")["pic"];
        $form->handleRequest($request);
    
        if ($form->isValid()) {
            // $file stores the uploaded PDF file
            /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
            $file = $pic;
    
            // Generate a unique name for the file before saving it
            $fileName = md5(uniqid()) . '.' . $pic->guessExtension();
    
            // Move the file to the directory where brochures are stored
            $brochuresDir = $this->container->getParameter('kernel.root_dir') . '/../web/uploads';
            copy($pic->getPathname(), $brochuresDir . "/" . $fileName);
    
            // Update the 'brochure' property to store the PDF file name
            // instead of its contents
            $product->setPic($fileName);
    
            // ... persist the $product variable or any other work
    
            return $this->redirect($this->generateUrl('app_product_new'));
        }
    
        return $this->render('FeliceAdminBundle:Default:index.html.twig', array(
            'form' => $form->createView(),
        ));
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-21
      • 2018-06-29
      • 2016-05-10
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多