2013-03-21 33 views
1

我想知道如何在不使用收集字段类型的情况下创建多个文件上传。事情是:我有一个班,我需要链接图片。我不需要这些图像中的任何描述和其他类型的信息,所以我只是在主实体中创建了额外的字段。然后,添加使用Symfony2上传和删除多个文件

->add('files', 'file', array(
     'label' => 'Images', 
     'required' => false, 
     'attr' => array(
      'accept' => 'image/*', 
      'multiple' => true 
     ) 

的窗体类和这样的:

/** 
* @ORM\PrePersist() 
* @ORM\PreUpdate() 
*/ 
public function preUpload() 
{ 
    if (!empty($this->files)) { 
     if (!empty($this->images)) { 
      $this->insertingKey = $this->images->count(); 
     } 
     foreach ($this->files as $file) { 
      $imageName = uniqid('entity_') . '_' . date('Y-m-d_H:i') . '.' . $file->guessExtension(); 
      if ($this->images === null) { 
       $this->images = new ArrayCollection(); 
      } 
      $this->images->add($imageName); 
     } 
    } 
} 

/** 
* @ORM\PostPersist() 
* @ORM\PostUpdate() 
*/ 
public function upload() 
{ 
    if (empty($this->files)) { 
     return; 
    } 

    if ($this->insertingKey) { 
     foreach ($this->files as $file) { 
      $file->move($this->getUploadRootDir(), $this->images[ $this->insertingKey++ ]); 
     } 
    } else { 
     foreach ($this->files as $key => $file) { 
      $file->move($this->getUploadRootDir(), $this->images[ $key ]); 
     } 
    } 
} 

的学说事件在我的实体类,当然我做喜欢和数组文件归档工作。 但我的问题是 - 我无法第二次添加图像。例如,当我已经上传了一些图片并决定上传更多图片时,他们会实际上传图片(我可以在我的项目目录中看到它们),但是我的网页上没有任何更改。

你能给我些建议吗?或者,也许还有其他方法通过上传许多文件并同时只有一个表单域?万分感谢。

回答

1

我这种方式解决了这个问题:

这是从我的实体代码

/** 
* @ORM\PrePersist() 
* @ORM\PreUpdate() 
*/ 
public function preUpload() 
{ 
    if ($this->files[0] != null || !$this->files) { 
     if (!empty($this->images)) { 
      $this->insertingKey = count($this->images); 
     } 
     foreach ($this->files as $file) { 
      $imageName = uniqid('pref_') . '_' . date('Y-m-d_H:i') . '.' . $file->guessExtension(); 
      if ($this->images === null) { 
       $this->images = array(); 
      } 
      $this->images[] = $imageName; 
     } 
    } 
} 

/** 
* @ORM\PostPersist() 
* @ORM\PostUpdate() 
*/ 
public function upload() 
{ 
    if ($this->files[0] == null || !$this->files) { 
     return; 
    } 

    if ($this->insertingKey) { 
     foreach ($this->files as $file) { 
      $file->move($this->getUploadRootDir(), $this->images[ $this->insertingKey++ ]); 
     } 
    } else { 
     foreach ($this->files as $key => $file) { 
      $file->move($this->getUploadRootDir(), $this->images[ $key ]); 
     } 
    } 
} 

public function getImagesWithAbsolutePath() 
{ 
    if (!empty($this->images)) { 
     $images = array(); 
     foreach ($this->images as $image) { 
      $images[$image] = $this->getUploadRootDir() . '/' . $image; 
     } 

     return $images; 
    } 

    return null; 
} 

public function getImagesWithRelativePath() 
{ 
    if (!empty($this->images)) { 
     $images = array(); 
     foreach ($this->images as $image) { 
      $images[$image] = $this->getUploadDir() . '/' . $image; 
     } 

     return $images; 
    } 

    return null; 
} 

public function getUploadRootDir() 
{ 
    return __DIR__ . '/../../../../web/' . $this->getUploadDir(); 
} 

public function getUploadDir() 
{ 
    return 'images/venue'; 
} 

public function removeImage($imageName) 
{ 
    if ($this->images && in_array($imageName, $this->images)) { 
     $key = array_search($imageName, $this->images); 
     unset($this->images[$key]); 
     if (file_exists($this->getUploadRootDir() . '/' . $imageName)) { 
      unlink($this->getUploadRootDir() . '/' . $imageName); 
     } 

     $this->images = array_values($this->images); 
    } 
} 

这是从控制器的一段代码:

if ($request->getMethod() === "POST") { 
     $form->bind($request); 
     if ($form->isValid()) { 
      $deleteImages = $request->request->get('delete_thumb', array()); 
      if (!empty($deleteImages)) { 
       foreach ($request->request->get('delete_thumb') as $image) { 
        $imageName = substr($image, strrpos($image, '/') + 1); 
        $venue->removeImage($imageName); 
       } 
      } 
      $this->persist($venue, true); 
      if ($request->request->get('submit-and-quit') !== null) { 
       return $this->redirectToRoute('admin_list'); 
      } 

      return array('form' => $form->createView()); 
     } 
    } 
相关问题