如何修改上传的照片symfony2

时间:2014-03-16 01:33:21

标签: php symfony file-upload doctrine-orm

我有Symfony 2.4网络应用程序,我需要更新照片,但是当我调用edit_form时,它总是为照片字段提供null,而不是在第一次创建时上传了照片,

所以问题是如何将照片作为文件传递给表单? 或者我应该选择的最佳做法是什么?

任何帮助将不胜感激。

<\ n>在EtudiantController

 private function createEditForm(Etudiant $entity)
{
  $form = $this->createForm(new EtudiantType(), $entity, array(
        'action' => $this->generateUrl('etudiant_update', array('id' => $entity->getId())),
        'method' => 'PUT',
    ));

    $form->add('submit', 'submit', array('label' => 'Update'));

    return $form;
}

和我的EtudiantType

->add('photo', 'file', array(
            'data_class' => null
        ))

和Etudiant.php

/**
 * @var string $photo
 * @Assert\File( maxSize = "1024k", mimeTypesMessage = "Please upload a valid Image")
 * @ORM\Column(name="photo", type="string", length=255)
 */
public $photo;


/**
 * Set photo
 *
 * @param string $photo
 * @return Animateur
 */
public function setPhoto($photo)
{
    $this->photo = $photo;

    return $this;
}

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


public function getFullImagePath() {
    return null === $this->photo ? null : $this->getUploadRootDir(). $this->photo;
}

protected function getUploadRootDir() {
    // the absolute directory path where uploaded documents should be saved
    return $this->getTmpUploadRootDir().$this->getId()."/";
}

protected function getTmpUploadRootDir() {
    // the absolute directory path where uploaded documents should be saved
    return __DIR__ . '/../../../../web/upload/';
}


public function uploadImage() {
    // the file property can be empty if the field is not required
    if (null === $this->photo) {
        return;
    }
    if(!$this->id){
        $this->photo->move($this->getTmpUploadRootDir(), $this->photo->getClientOriginalName());
    }else{
        $this->photo->move($this->getUploadRootDir(), $this->photo->getClientOriginalName());
    }
    $this->setPhoto($this->photo->getClientOriginalName());
}


public function moveImage()
{
    if (null === $this->photo) {
        return;
    }
    if(!is_dir($this->getUploadRootDir())){
        mkdir($this->getUploadRootDir());
    }
    copy($this->getTmpUploadRootDir().$this->photo, $this->getFullImagePath());
    unlink($this->getTmpUploadRootDir().$this->photo);
}


public function removeImage()
{
    unlink($this->getFullImagePath());
    rmdir($this->getUploadRootDir());
}


/**
 * @ORM\PostRemove
 */
public function removeUpload()
{
    // Add your code here
}

1 个答案:

答案 0 :(得分:0)

正如官方文档(http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html)中所建议的那样,您应该有两个字段,一个用于已上传和存储的图像,另一个用于表单处理:

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

/**
 * @Assert\File( maxSize = "1024k", mimeTypesMessage = "Please upload a valid Image")
 */
public $uploadedPhoto;

然后,您只需将“uploadedPhoto”字段添加到表单中:

->add('uploadedPhoto', 'file'))

如果您需要显示当前图像,只要从控制器传递,就可以从Twig模板访问Etudiant实体。表单提交并验证后,只需将上传的照片压缩到最终文件夹并使用新值更新photo属性。