php - 无法通过 Symfony 2.8 表单上传上传文件

标签 php forms symfony file-upload doctrine

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

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(),
    ));
}
}

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

但唯一的异常(exception)是我使用了 yml,而不是注解。毕竟,我在尝试上传文件时遇到错误:

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

我做错了什么?

最佳答案

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

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(),
    ));
}
}

关于php - 无法通过 Symfony 2.8 表单上传上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34768396/

相关文章:

php - 如何确定访问您网站的用户是否不是机器人?

nginx - PHP-FPM 在发生致命的 php 错误后提供空白页面

php - 使用 PHP、SimpleXML 和 Xpath 的 XML 提要和命名空间

php - 根据服务器请求将 PHP header 发送到 html 文件?

javascript - 表单上不可见的提交按钮

php - Symfony2 - 创建自己的供应商包 - 项目和 git 策略

django - 在 Django 的下拉列表中显示初始数据

css - 在页面上显示验证消息后,我的注册表单 CSS 中断了

php - FOSRestBundle:显示我的自定义异常消息

symfony - 服务 "fos_user.mailer"依赖于不存在的服务 "templating"