Skip to content

Commit e4e9bb8

Browse files
committed
Merge branch 'enhancing-doctrine-file-uploads' of github.com:mahmouds/symfony-docs into mahmouds-enhancing-doctrine-file-uploads
Conflicts: cookbook/doctrine/file_uploads.rst
2 parents 11b63e3 + 15ccf24 commit e4e9bb8

File tree

1 file changed

+106
-23
lines changed

1 file changed

+106
-23
lines changed

cookbook/doctrine/file_uploads.rst

Lines changed: 106 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,38 @@ look like this::
118118
}
119119

120120
Next, create this property on your ``Document`` class and add some validation
121-
rules:
121+
rules::
122+
123+
use Symfony\Component\HttpFoundation\File\UploadedFile;
124+
125+
// ...
126+
class Document
127+
{
128+
/**
129+
* @Assert\File(maxSize="6000000")
130+
*/
131+
private $file;
132+
133+
/**
134+
* Sets file.
135+
*
136+
* @param UploadedFile $file
137+
*/
138+
public function setFile(UploadedFile $file = null)
139+
{
140+
$this->file = $file;
141+
}
142+
143+
/**
144+
* Get file.
145+
*
146+
* @return UploadedFile
147+
*/
148+
public function getFile()
149+
{
150+
return $this->file;
151+
}
152+
}
122153
123154
.. configuration-block::
124155

@@ -144,7 +175,7 @@ rules:
144175
/**
145176
* @Assert\File(maxSize="6000000")
146177
*/
147-
public $file;
178+
private $file;
148179
149180
// ...
150181
}
@@ -273,7 +304,7 @@ object, which is what's returned after a ``file`` field is submitted::
273304
public function upload()
274305
{
275306
// the file property can be empty if the field is not required
276-
if (null === $this->file) {
307+
if (null === $this->getFile()) {
277308
return;
278309
}
279310

@@ -282,16 +313,16 @@ object, which is what's returned after a ``file`` field is submitted::
282313

283314
// move takes the target directory and then the
284315
// target filename to move to
285-
$this->file->move(
316+
$this->getFile()->move(
286317
$this->getUploadRootDir(),
287-
$this->file->getClientOriginalName()
318+
$this->getFile()->getClientOriginalName()
288319
);
289320

290321
// set the path property to the filename where you've saved the file
291-
$this->path = $this->file->getClientOriginalName();
322+
$this->path = $this->getFile()->getClientOriginalName();
292323

293324
// clean up the file property as you won't need it anymore
294-
$this->file = null;
325+
$this->setFile(null);
295326
}
296327

297328
Using Lifecycle Callbacks
@@ -329,16 +360,36 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
329360
*/
330361
class Document
331362
{
363+
private $temp;
364+
365+
/**
366+
* Sets file.
367+
*
368+
* @param UploadedFile $file
369+
*/
370+
public function setFile(UploadedFile $file = null)
371+
{
372+
$this->file = $file;
373+
// check if we have an old image path
374+
if (isset($this->path)) {
375+
// store the old name to delete after the update
376+
$this->temp = $this->path;
377+
$this->path = null;
378+
} else {
379+
$this->path = 'initial';
380+
}
381+
}
382+
332383
/**
333384
* @ORM\PrePersist()
334385
* @ORM\PreUpdate()
335386
*/
336387
public function preUpload()
337388
{
338-
if (null !== $this->file) {
389+
if (null !== $this->getFile()) {
339390
// do whatever you want to generate a unique name
340391
$filename = sha1(uniqid(mt_rand(), true));
341-
$this->path = $filename.'.'.$this->file->guessExtension();
392+
$this->path = $filename.'.'.$this->getFile()->guessExtension();
342393
}
343394
}
344395

@@ -348,16 +399,24 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
348399
*/
349400
public function upload()
350401
{
351-
if (null === $this->file) {
402+
if (null === $this->getFile()) {
352403
return;
353404
}
354405

355406
// if there is an error when moving the file, an exception will
356407
// be automatically thrown by move(). This will properly prevent
357408
// the entity from being persisted to the database on error
358-
$this->file->move($this->getUploadRootDir(), $this->path);
409+
$this->getFile()->move($this->getUploadRootDir(), $this->path);
359410

360-
unset($this->file);
411+
$this->setFile(null);
412+
413+
// check if we have an old image
414+
if (isset($this->temp)) {
415+
// delete the old image
416+
unlink($this->getUploadRootDir().'/'.$this->temp);
417+
// clear the temp image path
418+
$this->temp = null;
419+
}
361420
}
362421

363422
/**
@@ -418,17 +477,33 @@ property, instead of the actual filename::
418477
*/
419478
class Document
420479
{
421-
// a property used temporarily while deleting
422-
private $filenameForRemove;
480+
private $temp;
481+
482+
/**
483+
* Sets file.
484+
*
485+
* @param UploadedFile $file
486+
*/
487+
public function setFile(UploadedFile $file = null)
488+
{
489+
$this->file = $file;
490+
// check if we have an old image path
491+
if (is_file($this->getAbsolutePath())) {
492+
// store the old name to delete after the update
493+
$this->temp = $this->getAbsolutePath();
494+
} else {
495+
$this->path = 'initial';
496+
}
497+
}
423498

424499
/**
425500
* @ORM\PrePersist()
426501
* @ORM\PreUpdate()
427502
*/
428503
public function preUpload()
429504
{
430-
if (null !== $this->file) {
431-
$this->path = $this->file->guessExtension();
505+
if (null !== $this->getFile()) {
506+
$this->path = $this->getFile()->guessExtension();
432507
}
433508
}
434509

@@ -438,36 +513,44 @@ property, instead of the actual filename::
438513
*/
439514
public function upload()
440515
{
441-
if (null === $this->file) {
516+
if (null === $this->getFile()) {
442517
return;
443518
}
444519

520+
// check if we have an old image
521+
if (isset($this->temp)) {
522+
// delete the old image
523+
unlink($this->temp);
524+
// clear the temp image path
525+
$this->temp = null;
526+
}
527+
445528
// you must throw an exception here if the file cannot be moved
446529
// so that the entity is not persisted to the database
447530
// which the UploadedFile move() method does
448-
$this->file->move(
531+
$this->getFile()->move(
449532
$this->getUploadRootDir(),
450-
$this->id.'.'.$this->file->guessExtension()
533+
$this->id.'.'.$this->getFile()->guessExtension()
451534
);
452535

453-
unset($this->file);
536+
$this->setFile(null);
454537
}
455538

456539
/**
457540
* @ORM\PreRemove()
458541
*/
459542
public function storeFilenameForRemove()
460543
{
461-
$this->filenameForRemove = $this->getAbsolutePath();
544+
$this->temp = $this->getAbsolutePath();
462545
}
463546

464547
/**
465548
* @ORM\PostRemove()
466549
*/
467550
public function removeUpload()
468551
{
469-
if ($this->filenameForRemove) {
470-
unlink($this->filenameForRemove);
552+
if (isset($this->temp)) {
553+
unlink($this->temp);
471554
}
472555
}
473556

0 commit comments

Comments
 (0)