symfony2.8上的多上传文件



我在symfony2.8上做多上传文件,我发现总是有问题,我总是得到这个:

this is my entity/Article.php

<?php
namespace RoubBundleEntity;
use SymfonyComponentHttpFoundationFileFile;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentHttpFoundationFileUploadedFile;

/**
 * Article
 *
 * @ORMTable(name="article")
 * @ORMEntity(repositoryClass="RoubBundleRepositoryArticleRepository")
 */
class Article
{
/**
 * @var int
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @ORMColumn(type="string", nullable=true)
 * 
 * @AssertFile(
 *      maxSize="5242880",
 *      mimeTypes = {
 *          "image/png",
 *          "image/jpeg",
 *          "image/jpg",
 *          "image/gif"
 *      }
 * )
 */
public $image= array();
/**
 * @ORMColumn(type="string")
 * @var string
 */
private $titre;
public function getTitre()
{
    return $this->titre;
}
public function setTitre($titre)
{
    $this->titre = $titre;
    return $this;
}

public function getImage() {
    return $this->image;
}
public function setImage(array $image) {
    $this->image = $image;
}
/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}
}

这是我的动作NewAction在我的控制器/articlecontroller。php

public function newAction(Request $request)
{
    $article = new Article();
    $form = $this->createForm('RoubBundleFormArticleType', $article);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
    if (null !== $article->getImage) {   
    foreach($file as $article->getImage) {    
/** @var SymfonyComponentHttpFoundationFileUploadedFile $file */
        $file = $article->getImage();
        $fileName = md5(uniqid()).'.'.$file->guessExtension();
        $file->move(
            $this->getParameter('images_directory'),
            $fileName
        );
        array_push($article->getImage(), $fileName);
        }
        }
        $em = $this->getDoctrine()->getManager();
        $em->persist($article);
        $em->flush();
        return $this->redirectToRoute('article_show', array('id' => $article->getId()));
    }
    return $this->render('RoubBundle:article:new.html.twig', array(
        'article' => $article,
        'form' => $form->createView(),
    ));
}

这是form/ArticleType.php

    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('titre')    
        ->add('image', 'file', array(
                    'required' => false,
                        'data_class' => null,
                    "multiple" => "multiple"))    
    ;
}

我在我的twig/new.html.twig

    {{ form_start(form) }}
 {{ form_widget(form.titre) }} </br>                                         
 {{ form_widget(form.image, { 'attr': { 'multiple': 'multiple' } }) }}                            
                             </br>
    <input type="submit" value="Create" />
{{ form_end(form) }}
朋友们,我希望有人能帮助我,我真的很紧张谢谢大家

您的问题是$image在您的实体中是字符串类型:

@ORMColumn(type="string", nullable=true)

因此,您首先需要弄清楚如何在数据库中保存图像列表。这样做的好方法是创建一个名为article_images的新表(和实体),它看起来像这样,例如:

$id
$imageUrl
$articleId

如果你不想要另一个表,你可以尝试将你的图像数组保存为json格式。要做到这一点,请使用

 @ORMColumn(type="json")

设置$image字段

第二个问题是你的"移动"代码。您正在推值在数组的末尾,而您只想要"移动"的值。下面是它的样子:

 if ($article->getImage()) {   
    $movedImages = array();
    foreach($article->getImage() as $index=>$file) {    
        $fileName = md5(uniqid()).'.'.$file->guessExtension();
        $file->move(
            $this->getParameter('images_directory'),
            $fileName
        );
        array_push($movedImages, $fileName);
        }
        $article->setImage($movedImages);
        }
        $em = $this->getDoctrine()->getManager();
        $em->persist($article);
        $em->flush();
        return $this->redirectToRoute('article_show', array('id' => $article->getId()));
    }

相关内容

  • 没有找到相关文章

最新更新