首页 文章

在symfony 2中上传多个文件

提问于
浏览
-2

我想在项目Symfony 2中上传多个文件但是出现了错误

当您尝试向我添加多个文件时出现此错误

Image error

代码实体:

<?php

namespace CrudBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Actualite
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="CrudBundle\Entity\ActualiteRepository")
 */
class Actualite
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="nomactualite", type="string", length=255)
     */
    private $nomactualite;


    /**
     * @var string
     *
     * @ORM\Column(name="contenuactualite", type="string", length=255)
     */
    private $contenuactualite;

     /**
     * @ORM\OneToMany(targetEntity="Matche", mappedBy="actualites")
     */
    private $matches;

    /**
     * @ORM\ManyToOne(targetEntity="Equipe", inversedBy="actualite")
     * @ORM\JoinColumn(name="id_stade",referencedColumnName="id") 
     */
    private $equipe;

    /**
     * @ORM\OneToMany(targetEntity="Equipe", mappedBy="actualites")
     */
    private $equipe1;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    public $path;

     /**
     * @Assert\File(maxSize="6000000")
     */
    private $file;



    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set nomactualite
     *
     * @param string $nomactualite
     * @return Actualite
     */

    public function setNomactualite($nomactualite)
    {
        $this->nomactualite = $nomactualite;

        return $this;
    }

    /**
     * Get nomactualite
     *
     * @return string 
     */

    public function getNomactualite()
    {
        return $this->nomactualite;
    }

    /**
     * Set contenuactualite
     *
     * @param string $contenuactualite
     * @return Actualite
     */
    public function setContenuactualite($contenuactualite)
    {
        $this->contenuactualite = $contenuactualite;

        return $this;
    }

    /**
     * Get contenuactualite
     *
     * @return string 
     */
    public function getContenuactualite()
    {
        return $this->contenuactualite;
    }

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->matches = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add matches
     *
     * @param \CrudBundle\Entity\Matche $matches
     * @return Actualite
     */
    public function addMatch(\CrudBundle\Entity\Matche $matches)
    {
        $this->matches[] = $matches;

        return $this;
    }

    /**
     * Remove matches
     *
     * @param \CrudBundle\Entity\Matche $matches
     */
    public function removeMatch(\CrudBundle\Entity\Matche $matches)
    {
        $this->matches->removeElement($matches);
    }

    /**
     * Get matches
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getMatches()
    {
        return $this->matches;
    }


    /**
     * Set equipe
     *
     * @param \CrudBundle\Entity\Equipe $equipe
     * @return Actualite
     */
    public function setEquipe(\CrudBundle\Entity\Equipe $equipe = null)
    {
        $this->equipe = $equipe;

        return $this;
    }

    /**
     * Get equipe
     *
     * @return \CrudBundle\Entity\Equipe 
     */
    public function getEquipe()
    {
        return $this->equipe;
    }

    /**
     * Add equipe1
     *
     * @param \CrudBundle\Entity\Equipe $equipe1
     * @return Actualite
     */
    public function addEquipe1(\CrudBundle\Entity\Equipe $equipe1)
    {
        $this->equipe1[] = $equipe1;

        return $this;
    }

    /**
     * Remove equipe1
     *
     * @param \CrudBundle\Entity\Equipe $equipe1
     */
    public function removeEquipe1(\CrudBundle\Entity\Equipe $equipe1)
    {
        $this->equipe1->removeElement($equipe1);
    }

    /**
     * Get equipe1
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getEquipe1()
    {
        return $this->equipe1;
    }

    /**
 * @ORM\PreFlush()
 */


     public function getAbsolutePath()
    {
        return null === $this->path
            ? null
            : $this->getUploadRootDir().'/'.$this->path;
    }

    public function getWebPath()
    {
        return null === $this->path
            ? null
            : $this->getUploadDir().'/'.$this->path;
    }

    protected function getUploadRootDir()
    {
        // the absolute directory path where uploaded
        // documents should be saved
        return __DIR__.'/../../../web/'.$this->getUploadDir();
    }

    protected function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw up
        // when displaying uploaded doc/image in the view.
        return 'uploads/Actualite';
    }


    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
        public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
    }


    /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }

    public function upload()
{
    // the file property can be empty if the field is not required
    if (null === $this->getFile()) {
        return;
    }

    // use the original file name here but you should
    // sanitize it at least to avoid any security issues

    // move takes the target directory and then the
    // target filename to move to
    $this->getFile()->move(
        $this->getUploadRootDir(),
        $this->getFile()->getClientOriginalName()
    );

    // set the path property to the filename where you've saved the file
    $this->path = $this->getFile()->getClientOriginalName();

    // clean up the file property as you won't need it anymore
    $this->file = null;
}


    /**
     * Set path
     *
     * @param string $path
     * @return Article
     */
    public function setPath($path)
    {
        $this->path = $path;

        return $this;
    }

    /**
     * Get path
     *
     * @return string 
     */
    public function getPath()
    {
        return $this->path;
    }

}

代码控制器:

<?php

namespace CrudBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ActualiteType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nomactualite','text')
            ->add('contenuactualite', 'textarea', array('attr' => array('class' => 'ckeditor')))
           ->add('file', new FilesType(), array(
                'label'    =>    'Photos',
                'required' =>    true,
                'attr'     =>    array(
                'accept'  =>  'article/*', )
                ))

        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'CrudBundle\Entity\Actualite'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'crudbundle_actualite';
    }
}

代码形式:

<?php

namespace CrudBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ActualiteType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nomactualite','text')
            ->add('contenuactualite', 'textarea', array('attr' => array('class' => 'ckeditor')))
           ->add('file', new FilesType(), array(
                'label'    =>    'Photos',
                'required' =>    true,
                'attr'     =>    array(
                'accept'  =>  'article/*', )
                ))

        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'CrudBundle\Entity\Actualite'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'crudbundle_actualite';
    }
}

代码FileType:

<?php

    namespace CrudBundle\Form;

    use Symfony\Component\Form\Extension\Core\Type\FileType;
    use Symfony\Component\Form\FormInterface;
    use Symfony\Component\Form\FormView;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    use Symfony\Component\Validator\Constraints as Assert;
    /**
    * Class FilesType
    */
    class FilesType extends FileType
    {
        public function buildView(FormView $view, \Symfony\Component\Form\FormInterface $form, array $options)
        {
            parent::buildView($view, $form, $options);
            $view->vars['full_name'] .= '[]';
            $view->vars['attr']['multiple'] = 'multiple';
        }

        /**
        * {@inheritdoc}
        */
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            parent::setDefaultOptions($resolver);
            $resolver->setDefaults(
                array(
                    'data_class' => null,
                )
            );
        }

        public function getName()
        {
            return 'files';
        }
    }
?>

等待你的帮助

1 回答

  • 0

    错误是在\ CrudBundle \ Entity \ Actualite类中说这个方法:

    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
        public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
    }
    

    是获取数组(文件?)而不是单个文件 .

    您已修改(扩展) FileType 以使用多个文件,但您没有相应地修改 Actualite 类以接受它们 .

相关问题