1

I'm facing a weird issue : when I want to change the profil's picture of an user (using Symfony2), the function to upload the file is not call and so, my picture is not changed... But, when I create the user (the first time), the function is called and the picture is well uploaded.

Here is my code :

View :

<div class="btn btn-default image-preview-input">
    <span class="glyphicon glyphicon-folder-open"></span>
    <span class="image-preview-input-title">Choisir une photo de profil</span>
    {{ form_widget(form.logo) }}
</div>

Entity :

/**
 * @Assert\File(
 *              maxSize="6000000",
 *              mimeTypes={"image/png", "image/jpeg", "image/gif"}
 * )
 * 
 */
public $logo;

Upload function and other for upload :

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

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

protected function getUploadDir()
{
   return 'uploads/events';
}

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

    $path = $this->id . '.png';

    if(file_exists ($this->getUploadRootDir() . $path))
         unlink($this->getUploadRootDir() . $path);

    $this->logo->move($this->getUploadRootDir(), $path);

    $this->logo = null;
}

/**
 * @ORM\PreRemove()
 */
public function storeFilenameForRemove()
{
    $this->filenameForRemove = $this->getAbsolutePath();
}

/**
 * @ORM\PostRemove()
 */
public function removeUpload()
{
    if ($this->filenameForRemove) {
        unlink($this->filenameFor Remove);
    }
}

Have you got any idea of why, and how I can fix it ?

Thanks !

2 Answers 2

1

File upload not detected is Doctrine issue. Since file upload is not directly change the field where file name stored, Doctrine consider the entity as unchanged so it is ignored. This happens when only file upload is to be changed.

You can mark the entity changes in controller, like that:

if ($editForm['fileUpload']->getData() !== null) {
    $entity->setLogo("logo_".uniqid(mt_rand(), true));
}

Or create a field for entity modification time, and force it to change on controller update action:

// force unit of work to detect entity 'changes'
$entity->setModificationTime(new \DateTime());
Sign up to request clarification or add additional context in comments.

1 Comment

ok... I'll try it later, and if it works, I'll confirm your answer ! Thanks
0

try removing the old image first before saving the new one, it may be some permission error since it already exist for me i always did it like :

        if (isset($this->logo) && $this->logo!= null) {
            $old_img = $this->getUploadRootDir().'/'.$this->logo;
            if (file_exists($old_img))
                unlink($old_img);
        }

Otherwise you should check if the form is valid and that the upload function runs as expected with some debugging.

2 Comments

Thanks for your answer, but it's already what I do : if(file_exists ($this->getUploadRootDir() . $path)) unlink($this->getUploadRootDir() . $path); My problem is that the function where this code is, is never call
yeah true, then the next step is to debug and see where is the problem, is the form valid etc.. maybe some if doesnt go through for some reason you need to check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.