2013-03-21 2 views
1

컬렉션 필드 유형을 사용하지 않고 여러 파일을 업로드하는 방법을 궁금합니다. 문제는 : 클래스가 있고이 클래스를 사용하여 이미지를 링크해야합니다. 이 이미지에는 설명이나 다른 종류의 정보가 필요하지 않으므로 필자는 주체에 추가 필드를 만들었습니다. 내 엔티티 클래스의 교리 이벤트로Symfony2로 여러 파일 업로드 및 제거

/** 
* @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 ]); 
     } 
    } 
} 

, 물론 내가 좋아하는 어레이 파일 제출 한 작품을 만들 : 그럼 거기에

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

양식 클래스와이있어 추가합니다. 하지만 내 문제는 - 두 번째 이미지를 추가 할 수 없습니다. 예를 들어, 이미 이미지를 업로드하고 더 업로드하기로 결정한 경우 물리적으로 업로드됩니다 (프로젝트 디렉토리에서 볼 수 있음).하지만 내 페이지에서 변경된 사항은 없습니다.

조언을 좀 주시겠습니까? 또는 여러 파일을 업로드하고 동시에 하나의 양식 필드 만 갖는 다른 방법이 있습니까? 대단히 감사합니다.

나는이 문제를 이런 식으로 해결했습니다

답변

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()); 
     } 
    } 
관련 문제