当添加第二个文件字段时,Symfony2文件上传失败 [英] Symfony2 file upload fails when second file field is added
问题描述
我正在尝试上传2张图片与2个表单字段。我的形式:
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)'))
实体:
/**
* @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;
}
控制器:
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(),
));
}
配置:
parameters:
locale: en
img_directory: '%kernel.root_dir%/../web/uploads/img'
当我仅使用1个图像上传字段时,一切都OK,但现在获取文件C:\xampp\tmp\ phpBF79.tmp不存在错误,谁知道有什么问题?
提前感谢您的帮助。
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.
编辑:添加了我的html / twig表单
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 %}
推荐答案
怀疑 uniqid
正在为两次上传生成相同的文件名,这是您看到的文件未找到问题的核心。
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.
从PHP文档:
uniqid
基于当前时间(以微秒为单位)获取前缀唯一标识符。 / p>
uniqid
Gets a prefixed unique identifier based on the current time in microseconds.
对 uniqid
的调用都基于时间 - 在微数秒的功能部分,它可能会分配它们两个相同的名称。因此,当您进入 $ file-> move
时,第二个将会丢失。
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.
尝试在 mt_rand
,文件名冲突的可能性要小得多。您还可以通过调用 $ file
上的 getBasename
来缓解这种可能性,并将其连接到传递给 md5
。
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
.
这篇关于当添加第二个文件字段时,Symfony2文件上传失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!