doucai6663 2016-10-18 03:03
浏览 46
已采纳

添加第二个文件字段时,Symfony2文件上载失败

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

展开全部

  • 写回答

3条回答 默认 最新

  • dtgvl48608 2016-10-19 06:08
    关注

    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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部