Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 80 additions & 6 deletions cookbook/doctrine/file_uploads.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

// ...
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -397,16 +471,16 @@ property, instead of the actual filename::
*/
public function storeFilenameForRemove()
{
$this->filenameForRemove = $this->getAbsolutePath();
$this->temp = $this->getAbsolutePath();
}

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

Expand Down