Symfony多个图像上传仅发现2张图片

时间:2017-01-30 16:31:06

标签: php image symfony symfony-forms

我尝试使用Symfony3

上传多个表单的多个图片

提交后我收到此错误:

  

文件" C:\ Users \ aykon \ AppData \ Local \ Temp \ phpE956.tmp"才不是   存在

     

CRITICAL - 未捕获的PHP异常   Symfony的\分量\ HttpFoundation \文件\异常\ FileNotFoundException异常:   "文件" C:\ Users \ aykon \ AppData \ Local \ Temp \ phpE956.tmp"才不是   存在"在   C:\ Users \用户aykon \ PhpstormProjects \ viamia \供应商\ symfony的\ symfony中的\ src \的Symfony \分量\ HttpFoundation \文件\ Mime类型\ MimeTypeGuesser.php   第123行

这在成功上传第二张图片后的for循环中发生

对不起代码,但我通过多种方式尝试使用数组和虚拟代码,所以这是另一个具有相同代码的虚拟代码

public function homepageSecondManagementAction(Request $request)
    {
        $homepageENAll = $this->getDoctrine()->getRepository('ContentManagementSystemBundle:HomepageEN')->findAll();
        $homepageCSAll = $this->getDoctrine()->getRepository('ContentManagementSystemBundle:HomepageCS')->findAll();

        $homepageEN = [];
        $homepageCS = [];
        for ($x = 2; $x < 5; $x++) {
            $homepageEN[] = $homepageENAll[$x];
            $homepageCS[] = $homepageCSAll[$x];
        }

        $block1ENForm = $this->createForm(HomepageBlckHdrDsshENForm::class, $homepageEN[0]);
        $block1CSForm = $this->createForm(HomepageBlckHdrDsshCSForm::class, $homepageCS[0]);
        for ($x = 2; $x < 4; $x++) {
            ${"block" . $x . "ENForm"} = $this->createForm(HomepageHdrDsshENForm::class, $homepageEN[$x - 1]);
            ${"block" . $x . "CSForm"} = $this->createForm(HomepageHdrDsshCSForm::class, $homepageCS[$x - 1]);
        }

        $counter = 1;

        foreach ($homepageEN as $key => $value) {
            ${"image" . $counter} = $value->getHomepageImageMain()[0];
            ${"file" . $counter} = (!empty(${"image" . $counter}) ? ${"image" . $counter}->getFile() : null);
            ${"imageForm" . $counter} = $this->createForm(ImageUploadForm::class, ${"image" . $counter});
            $counter++;
        }

        for ($x = 1; $x < 4; $x++) {
            ${"block" . $x . "ENForm"}->handleRequest($request);
            ${"block" . $x . "CSForm"}->handleRequest($request);
            ${"imageForm" . $x}->handleRequest($request);
        }

        $validForm = true;

        for ($x = 1; $x < 4; $x++) {
            if (!${"block" . $x . "ENForm"}->isSubmitted() && !${"block" . $x . "CSForm"}->isSubmitted() &&
                !${"imageForm" . $x}->isSubmitted()
            ) {
                $validForm = false;
            }
        }

        if ($validForm) {
            $entityManager = $this->getDoctrine()->getManager();

            for ($x = 1; $x < 4; $x++) {
                ${"block" . $x . "EN"} = ${"block" . $x . "ENForm"}->getData();
                ${"block" . $x . "CS"} = ${"block" . $x . "CSForm"}->getData();
                ${"block" . $x . "EN"}->setBlockName($block1EN->getBlockName());
                ${"block" . $x . "CS"}->setBlockName($block1CS->getBlockName());
                $entityManager->persist(${"block" . $x . "EN"});
                $entityManager->persist(${"block" . $x . "CS"});

                ${"image" . $x} = ${"imageForm" . $x}->getData();
                dump(${"image" . $x});
                if (${"image" . $x}->getFile() != null) {
                    ${"file" . $x} = ${"image" . $x}->getFile();
                    ${"fileName" . $x} = $this->get('app.image_uploader')->upload(${"file" . $x});
                    ${"image" . $x}->setFile(${"fileName" . $x});
                    ${"image" . $x}->setHomepageImageMain($homepageEN[$x - 1]);
                    $entityManager->persist(${"image" . $x});
                }
            }

            $entityManager->flush();

            $this->addFlash('success', 'Edited');

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

        return $this->render('backEnd/editSecondHomepage.html.twig', [
            'block1EN' => $block1ENForm->createView(),
            'block1CS' => $block1CSForm->createView(),
            'block2EN' => $block2ENForm->createView(),
            'block2CS' => $block2CSForm->createView(),
            'block3EN' => $block3ENForm->createView(),
            'block3CS' => $block3CSForm->createView(),
            'imageForm1' => $imageForm1->createView(),
            'imageForm2' => $imageForm2->createView(),
            'imageForm3' => $imageForm3->createView()
        ]);
    }

主页格式无图像

有图像表格

class ImageUploadForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('file', FileType::class, array(
                'required' => false,
                'data_class' => null,
                'property_path' => 'file'
            ))
            ->add('alternative', TextType::class, array(
                'required' => false,
                'empty_data' => false))
            ->add('imgTitle', TextType::class, array(
                'required' => false,
                'empty_data' => false))
            ->add('imgDescription', TextareaType::class, array(
                'required' => false,
                'empty_data' => false));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Image::class
        ]);
    }
}

并且有一部分Image实体

**
 * @ORM\Entity
 * @ORM\Table(name="image")
 * @ORM\HasLifecycleCallbacks
 */
class Image
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     * @Assert\File(
     *     maxSize = "5M",
     *     mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"},
     *     maxSizeMessage = "The maximum allowed file size is 5MB.",
     *     mimeTypesMessage = "Only image is allowed."
     * )
     */
    protected $file;

这是上传者,上传者正在处理一个被测试的文件

use Symfony\Component\HttpFoundation\File\UploadedFile;

class FileUploader
{
    private $targetDir;

    public function __construct($targetDir)
    {
        $this->targetDir = $targetDir;
    }

    public function upload(UploadedFile $file)
    {
        $fileName = md5(mt_rand()) . '.' . $file->guessExtension();

        $file->move($this->targetDir, $fileName);

        return $fileName;
    }
}

我应该如何成功上传并保留2张以上的图片?

1 个答案:

答案 0 :(得分:0)

第三个文件的扩展名看起来像.tmp:“ phpE956.tmp ”。 但是file属性中的注释表明您只接受jpeg,gif,png或tiff。

检查您的文件是否低于5Mo

您是否在php.ini中设置了upload_max_filesizepost_max_size的值?