diff --git a/cookbook/doctrine/file_uploads.rst b/cookbook/doctrine/file_uploads.rst index 287f8104459..e7397e8d08f 100644 --- a/cookbook/doctrine/file_uploads.rst +++ b/cookbook/doctrine/file_uploads.rst @@ -122,13 +122,35 @@ rules:: // src/Acme/DemoBundle/Entity/Document.php + use Symfony\Component\HttpFoundation\File\UploadedFile; + // ... class Document { /** * @Assert\File(maxSize="6000000") */ - public $file; + private $file; + + /** + * Sets file. + * + * @param UploadedFile $file + */ + public function setFile(UploadedFile $file) + { + $this->file = $file; + } + + /** + * Get file. + * + * @return UploadedFile + */ + public function getFile() + { + return $this->file; + } // ... } @@ -268,6 +290,26 @@ Next, refactor the ``Document`` class to take advantage of these callbacks:: */ class Document { + private $temp; + + /** + * Sets file. + * + * @param UploadedFile $file + */ + public function setFile(UploadedFile $file) + { + $this->file = $file; + // check if we have an old image path + if (isset($this->path)) { + // store the old name to delete after the update + $this->temp = $this->path; + $this->path = null; + } else { + $this->path = 'initial'; + } + } + /** * @ORM\PrePersist() * @ORM\PreUpdate() @@ -297,6 +339,14 @@ Next, refactor the ``Document`` class to take advantage of these callbacks:: $this->file->move($this->getUploadRootDir(), $this->path); unset($this->file); + + // check if we have an old image + if (isset($this->temp)) { + // delete the old image + unlink($this->getUploadRootDir().'/'.$this->temp); + // clear the temp image path + $this->temp = null; + } } /** @@ -357,8 +407,24 @@ property, instead of the actual filename:: */ class Document { - // a property used temporarily while deleting - private $filenameForRemove; + private $temp; + + /** + * Sets file. + * + * @param UploadedFile $file + */ + public function setFile(UploadedFile $file) + { + $this->file = $file; + // check if we have an old image path + if (is_file($this->getAbsolutePath())) { + // store the old name to delete after the update + $this->temp = $this->getAbsolutePath(); + } else { + $this->path = 'initial'; + } + } /** * @ORM\PrePersist() @@ -381,6 +447,14 @@ property, instead of the actual filename:: return; } + // check if we have an old image + if (isset($this->temp)) { + // delete the old image + unlink($this->temp); + // clear the temp image path + $this->temp = null; + } + // you must throw an exception here if the file cannot be moved // so that the entity is not persisted to the database // which the UploadedFile move() method does @@ -397,7 +471,7 @@ property, instead of the actual filename:: */ public function storeFilenameForRemove() { - $this->filenameForRemove = $this->getAbsolutePath(); + $this->temp = $this->getAbsolutePath(); } /** @@ -405,8 +479,8 @@ property, instead of the actual filename:: */ public function removeUpload() { - if ($this->filenameForRemove) { - unlink($this->filenameForRemove); + if (isset($this->temp)) { + unlink($this->temp); } }