Php Symfony2中的多个上载文件

Php Symfony2中的多个上载文件,php,symfony,upload,entity,Php,Symfony,Upload,Entity,我尝试在Symfony2中上载多个文件。我需要在不在controller中处理的情况下执行此操作,我尝试在正确的生命周期中执行此操作,但我无法创建实体的多个实例,我认为我需要修改,方法: /** * Sets file. * * @param UploadedFile $photo */ public function setPhoto(UploadedFile $photo = null) { $this->photo = $photo; if (iss

我尝试在Symfony2中上载多个文件。我需要在不在controller中处理的情况下执行此操作,我尝试在正确的生命周期中执行此操作,但我无法创建实体的多个实例,我认为我需要修改,方法:

    /**
 * Sets file.
 *
 * @param UploadedFile $photo
 */
public function setPhoto(UploadedFile $photo = null)
{

    $this->photo = $photo;
    if (isset($this->path)) {
        $this->temp = $this->path;
        $this->path = null;
    } else {
        $this->path = 'initial';
    }
}
对这样的事情:

    /**
 * Sets file.
 *
 * @param ArrayCollection $photo
 */
public function setPhoto(ArrayCollection $photo = null)
{
    foreach ($photo as $photos) {
        $file = new UploadedFile();
        $this->photo = $file;
    }

}

我是新手,请帮忙。

使用Symfony2进行多文件上传很棘手。我使用PHP uploads和Doctrine上传多个文件:

if(isset($_FILES['files']))
    {
        $errors= array();
        foreach($_FILES['files']['tmp_name'] as $key => $tmp_name )
        {
            $file_name = $_FILES['files']['name'][$key];
            $file_size =$_FILES['files']['size'][$key];
            $file_tmp =$_FILES['files']['tmp_name'][$key];
            $file_type=$_FILES['files']['type'][$key];
            if($file_size > 6097152) //whatever size you want
            {
                $errors[]='File size must be less than 6 MB';
            }
            $entity = new Entity; //your file entity

            //Do whatever you want with the entity...

            $request = $this->get('request');
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush(); 
            $desired_dir=__DIR__.'/../../../../web/[Your desired folder]/'.$entity->getId()."/";
            if(empty($errors)==true)
            {
                if(is_dir($desired_dir)==false)
                {
                    mkdir("$desired_dir", 0700);        // Create directory if it does not exist
                }
                if(is_dir("$desired_dir/".$file_name)==false)
                {
                    move_uploaded_file($file_tmp,__DIR__.'/../../../../web/[Your desired folder]/'.$entity->getId()."/".$file_name);
                }
                else
                {                                  //rename the file if another one exist
                    $new_dir=__DIR__.'/../../../../web/Flightpics/'.$flight->getId()."/".$file_name.time();
                    rename($file_tmp,$new_dir) ;
                }
            }
            else
            {
                //print errors if needed
            }
        }
        if(empty($error)){
                //print success if needed
        }
    }
所有文件都将被处理到您的web/[您的文件夹]/[您的实体id]文件夹中。在HTML代码中,表单应如下所示:

<form action="" method="POST" enctype="multipart/form-data">
                    <input type="file" name="files[]" multiple/ required>
                    <hr>
                    <input type="submit"/ value="Ajouter" class="btn btn-success">
                    </form>
不要忘记name=files[]部分以获取文件数组