Symfony2 file upload fails when second file field is added

為{幸葍}努か 提交于 2020-01-05 04:18:09

问题


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 %}

回答1:


I have a suspicion that uniqid is generating the same filename for both uploads, and that that is the heart of the file-not-found issue you're seeing.

From the PHP docs:

uniqid

Gets a prefixed unique identifier based on the current time in microseconds.

Both calls to uniqid are executed close enough together that based on the time-in-microseconds part of the function, it might assign them both the same name. Therefore, the second one will be "missing" when you go to $file->move it.

Try subbing in mt_rand, for a far lesser likelihood of file name collisions. You can also mitigate this possibility by calling getBasename on the $file, and concatenating that to the string passed to md5.




回答2:


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.




回答3:


I think it not solve your problem but you check in file2name extension from file not from file2. If you move file to another place maybe it's problem with guessExtension() on moved file.



来源:https://stackoverflow.com/questions/40106506/symfony2-file-upload-fails-when-second-file-field-is-added

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!