PawełP PawełP - 1 month ago 6
Twig Question

Symfony2 file upload fails when second file field is added

I'm trying to upload 2 images with 2 form fields. My form:

->add('zdjecie', FileType::class, array('label' => 'Zdjecie (img file)'))
->add('zdjecieMIN', FileType::class, array('label' => 'Zdjecie miniatura (img file)'))


Entity:

/**
* @ORM\Column(type="string")
*
* @Assert\NotBlank(message="Dodaj zdjecie miniaturke")
* @Assert\File(mimeTypes={"image/png", "image/jpeg", "image/jpg",})
*/
private $zdjecieMIN;

public function getZdjecieMIN()
{
return $this->zdjecieMIN;
}

public function setZdjecieMIN($zdjecieMIN)
{
$this->zdjecieMIN = $zdjecieMIN;

return $this;
}

/**
* @ORM\Column(type="string")
*
* @Assert\NotBlank(message="Dodaj zdjecie")
* @Assert\File(mimeTypes={"image/png", "image/jpeg", "image/jpg",})
*/
private $zdjecie;

public function getZdjecie()
{
return $this->zdjecie;
}

public function setZdjecie($zdjecie)
{
$this->zdjecie = $zdjecie;

return $this;
}


Controller:

public function newAction(Request $request)
{
$buty = new Buty();
$form = $this->createForm('ShoeShopBundle\Form\ButyType', $buty);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$file = $buty->getZdjecie();
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move(
$this->getParameter('img_directory'),
$fileName
);
$buty->setZdjecie($fileName);

$file2 = $buty->getZdjecieMIN();
$fileName2 = md5(uniqid()).'.'.$file->guessExtension();
$file2->move(
$this->getParameter('img_directory'),
$fileName2
);
$buty->setZdjecieMIN($fileName2);

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

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

return $this->render('ShoeShopBundle:Admin/Buty:new.html.twig', array(
'buty' => $buty,
'form' => $form->createView(),
));
}


Config:

parameters:
locale: en
img_directory: '%kernel.root_dir%/../web/uploads/img'


Everything was ok when I was using only 1 image upload field but now im getting "The file "C:\xampp\tmp\phpBF79.tmp" does not exist " error, anyone know what's wrong?
Thanks in advance for your help.

Edit: Added my html/twig form

{% extends 'base.html.twig' %}

{% block body %}
<div class="adm-new">
<h2>Dodaj nowy produkt</h2>

{{ form_start(form) }}
<div>
{{ form_errors(form.marka) }}
<div>
<div>
{{ form_label(form.marka) }}:
</div>
</div>

<div>
<div>
{{ form_widget(form.marka) }}
</div>
</div>
</div>
<div>
{{ form_errors(form.model) }}
<div>
<div>
{{ form_label(form.model) }}:
</div>
</div>

<div>
<div>
{{ form_widget(form.model) }}
</div>
</div>
</div>
<div>
{{ form_errors(form.kolor) }}
<div>
<div>
{{ form_label(form.kolor) }}:
</div>
</div>

<div>
<div>
{{ form_widget(form.kolor) }}
</div>
</div>
</div>
<div>
{{ form_errors(form.cena) }}
<div>
<div>
{{ form_label(form.cena) }}:
</div>
</div>

<div>
<div>
{{ form_widget(form.cena) }}
</div>
</div>
</div>
<div>
{{ form_errors(form.rozmiar) }}
<div>
<div>
{{ form_label(form.rozmiar) }}:
</div>
</div>

<div>
<div>
{{ form_widget(form.rozmiar) }}
</div>
</div>
</div>
<div>
{{ form_errors(form.zdjecieMIN) }}
<div>
<div>
{{ form_label(form.zdjecieMIN) }}:
</div>
</div>

<div>
<div>
{{ form_widget(form.zdjecieMIN) }}
</div>
</div>
</div>
<div>
{{ form_errors(form.zdjecie) }}
<div>
<div>
{{ form_label(form.zdjecie) }}:
</div>
</div>

<div>
<div>
{{ form_widget(form.zdjecie) }}
</div>
</div>
</div>
<div><input type="submit" value="Dodaj" /></div>
{{ form_end(form) }}

<ul>
<li>
<a href="{{ path('app_admin_buty_index') }}">Powrot do listy produktow</a>
</li>
</ul>
</div>
{% endblock %}

Answer

Like Camerun Hurd suggested it was a problem with uniqid generating the same filename for both files. mt_rand() function gave the same result so I just changed

$file2 = $buty->getZdjecieMIN();
    $fileName2 = md5(uniqid()).'.'.$file->guessExtension();
    $file2->move(
        $this->getParameter('img_directory'),
        $fileName2
    );
    $buty->setZdjecieMIN($fileName2);

in my controller to:

$ext_pos = strrpos($fileName, '.');

        $file2 = $buty->getZdjecieMIN();
        $fileName2 = substr($fileName, 0, $ext_pos) . '_min' . substr($fileName, $ext_pos);;
        $file2->move(
            $this->getParameter('img_directory'),
            $fileName2
        );
        $buty->setZdjecieMIN($fileName2);

Thanks Cameron and rodd from #symfony for all the help.