无法找到文件Symfony上传

时间:2016-10-27 21:01:39

标签: php symfony

我的实体中有两个字段用作文件的文件名:合同和许可证。我在第A页编写了合同上传,然后将其复制并更改为第B页上的许可。页面A与合同上传工作,页面B与完全相同的过程我得到一般错误“无法找到该文件。 “日志中没有任何内容。

来自实体(合同有效,许可证没有):

/**
 * @var
 *
 * @ORM\Column(name="contract", type="string", nullable=true)
 * @Assert\File(
 *     maxSize="600000",
 *     maxSizeMessage="The file is too large. Allowed maximum size is 8MB",
 *     mimeTypes={"image/jpg", "image/png", "image/gif", "image/jpeg", "application/pdf"}
 * )
 */
private $contract;

/**
 * @var
 *
 * @ORM\Column(name="license", type="string", nullable=true)
 * @Assert\File(
 *     maxSize="600000",
 *     maxSizeMessage="The file is too large. Allowed maximum size is 8MB",
 *     mimeTypes={"image/jpg", "image/png", "image/gif", "image/jpeg", "application/pdf"}
 * )
 */
private $license;

从控制器(contractAction上传,submit上的uploadLicenseAction显示上面列出的错误,不会使其进入$form->isValid()条件):

/**
 * @Route("/contract", name="contract")
 * @Template()
 * @param Request $request
 *
 * @return array
 */
public function contractAction(Request $request) {
    $em         = $this->getDoctrine()->getManager();
    $user       = $this->getUser();
    $store = $this->getDoctrine()->getRepository('ShadowBundle:Store')->find($user->getId());

    // If already signed, redirect to upload license page
    if (!empty($store->getContract())) {
        return $this->redirectToRoute('store_license_upload');
    }

    $form = $this->createFormBuilder($store)
        ->add('contract', FileType::class, [
            'required'   => true,
            'data_class' => null
        ])
        ->add('upload_contract', SubmitType::class, [
            'attr' => ['class' => 'btn btn-primary']
        ])
        ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        $file_dir = $this->container->getParameter('kernel.root_dir') . "/../web/images/uploads/store/contract";

        /**
         * @var UploadedFile $contract
         */
        $contract = $store->getContract();
        $filename = uniqid() . $contract->getClientOriginalName();

        $contract->move($file_dir, $filename);
        $store->setContract($filename);

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

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

    return [
        'form' => $form->createView()
    ];
}

/**
 * @Route("/license_upload", name="store_license_upload")
 * @Template()
 *
 * @param Request $request
 *
 * @return array
 */
public function uploadLicenseAction(Request $request) {
    $em         = $this->getDoctrine()->getManager();
    $user       = $this->getUser();
    $store= $this->getDoctrine()->getRepository('ShadowBundle:Store')->find($user->getId());

    $form = $this->createFormBuilder($store)
        ->add('license', FileType::class, [
            'data_class' => null,
            'required'   => false
        ])->add('upload_license', SubmitType::class, [
            'attr' => ['class' => 'btn btn-primary']
        ])->getForm();

    $form->handleRequest($request);

    if ($form->isValid()){
        exit(dump('test'));
    }

    return [
        'form' => $form->createView()
    ];
}

这个视图只是简单的html输出完整的表单{{ form(form) }}

这似乎与@Assert有关,因为如果删除它,它可以正常工作。如果我删除工作合同上的@Assert\File并尝试再次提交许可证,那么它可以工作,因为它与同一实体中的两个断言有关,但我有另一个实体也有两个{{1}那个工作没有问题。

思考?我知道我可以在表单声明中声明,但我真的不想脱离我为了一致性而做的方式。

编辑:即使从实体中删除断言并将其添加到窗体的约束中仍然无法正常工作,我得到了同样的错误。

0 个答案:

没有答案