@@ -118,7 +118,38 @@ look like this::
118
118
}
119
119
120
120
Next, create this property on your ``Document `` class and add some validation
121
- rules:
121
+ rules::
122
+
123
+ use Symfony\C omponent\H ttpFoundation\F ile\U ploadedFile;
124
+
125
+ // ...
126
+ class Document
127
+ {
128
+ /**
129
+ * @Assert\F ile(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
+ }
122
153
123
154
.. configuration-block ::
124
155
@@ -144,7 +175,7 @@ rules:
144
175
/**
145
176
* @Assert\File(maxSize="6000000")
146
177
*/
147
- public $file;
178
+ private $file;
148
179
149
180
// ...
150
181
}
@@ -273,7 +304,7 @@ object, which is what's returned after a ``file`` field is submitted::
273
304
public function upload()
274
305
{
275
306
// the file property can be empty if the field is not required
276
- if (null === $this->file ) {
307
+ if (null === $this->getFile() ) {
277
308
return;
278
309
}
279
310
@@ -282,16 +313,16 @@ object, which is what's returned after a ``file`` field is submitted::
282
313
283
314
// move takes the target directory and then the
284
315
// target filename to move to
285
- $this->file ->move(
316
+ $this->getFile() ->move(
286
317
$this->getUploadRootDir(),
287
- $this->file ->getClientOriginalName()
318
+ $this->getFile() ->getClientOriginalName()
288
319
);
289
320
290
321
// 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();
292
323
293
324
// clean up the file property as you won't need it anymore
294
- $this->file = null;
325
+ $this->setFile( null) ;
295
326
}
296
327
297
328
Using Lifecycle Callbacks
@@ -329,16 +360,36 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
329
360
*/
330
361
class Document
331
362
{
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
+
332
383
/**
333
384
* @ORM\PrePersist()
334
385
* @ORM\PreUpdate()
335
386
*/
336
387
public function preUpload()
337
388
{
338
- if (null !== $this->file ) {
389
+ if (null !== $this->getFile() ) {
339
390
// do whatever you want to generate a unique name
340
391
$filename = sha1(uniqid(mt_rand(), true));
341
- $this->path = $filename.'.'.$this->file ->guessExtension();
392
+ $this->path = $filename.'.'.$this->getFile() ->guessExtension();
342
393
}
343
394
}
344
395
@@ -348,16 +399,24 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
348
399
*/
349
400
public function upload()
350
401
{
351
- if (null === $this->file ) {
402
+ if (null === $this->getFile() ) {
352
403
return;
353
404
}
354
405
355
406
// if there is an error when moving the file, an exception will
356
407
// be automatically thrown by move(). This will properly prevent
357
408
// 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);
359
410
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
+ }
361
420
}
362
421
363
422
/**
@@ -418,17 +477,33 @@ property, instead of the actual filename::
418
477
*/
419
478
class Document
420
479
{
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
+ }
423
498
424
499
/**
425
500
* @ORM\PrePersist()
426
501
* @ORM\PreUpdate()
427
502
*/
428
503
public function preUpload()
429
504
{
430
- if (null !== $this->file ) {
431
- $this->path = $this->file ->guessExtension();
505
+ if (null !== $this->getFile() ) {
506
+ $this->path = $this->getFile() ->guessExtension();
432
507
}
433
508
}
434
509
@@ -438,36 +513,44 @@ property, instead of the actual filename::
438
513
*/
439
514
public function upload()
440
515
{
441
- if (null === $this->file ) {
516
+ if (null === $this->getFile() ) {
442
517
return;
443
518
}
444
519
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
+
445
528
// you must throw an exception here if the file cannot be moved
446
529
// so that the entity is not persisted to the database
447
530
// which the UploadedFile move() method does
448
- $this->file ->move(
531
+ $this->getFile() ->move(
449
532
$this->getUploadRootDir(),
450
- $this->id.'.'.$this->file ->guessExtension()
533
+ $this->id.'.'.$this->getFile() ->guessExtension()
451
534
);
452
535
453
- unset( $this->file );
536
+ $this->setFile(null );
454
537
}
455
538
456
539
/**
457
540
* @ORM\PreRemove()
458
541
*/
459
542
public function storeFilenameForRemove()
460
543
{
461
- $this->filenameForRemove = $this->getAbsolutePath();
544
+ $this->temp = $this->getAbsolutePath();
462
545
}
463
546
464
547
/**
465
548
* @ORM\PostRemove()
466
549
*/
467
550
public function removeUpload()
468
551
{
469
- if ($this->filenameForRemove ) {
470
- unlink($this->filenameForRemove );
552
+ if (isset( $this->temp) ) {
553
+ unlink($this->temp );
471
554
}
472
555
}
473
556
0 commit comments