@@ -122,13 +122,35 @@ rules::
122
122
123
123
// src/Acme/DemoBundle/Entity/Document.php
124
124
125
+ use Symfony\Component\HttpFoundation\File\UploadedFile;
126
+
125
127
// ...
126
128
class Document
127
129
{
128
130
/**
129
131
* @Assert\File(maxSize="6000000")
130
132
*/
131
- public $file;
133
+ private $file;
134
+
135
+ /**
136
+ * Set file
137
+ *
138
+ * @param UploadedFile $file
139
+ */
140
+ public function setFile(UploadedFile $file)
141
+ {
142
+ $this->file = $file;
143
+ }
144
+
145
+ /**
146
+ * Get file
147
+ *
148
+ * @return UploadedFile
149
+ */
150
+ public function getFile()
151
+ {
152
+ return $this->file;
153
+ }
132
154
133
155
// ...
134
156
}
@@ -268,6 +290,24 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
268
290
*/
269
291
class Document
270
292
{
293
+ private $temp;
294
+
295
+ /**
296
+ * Set file
297
+ *
298
+ * @param UploadedFile $file
299
+ */
300
+ public function setFile(UploadedFile $file)
301
+ {
302
+ $this->file = $file;
303
+ // check if we have an old image path
304
+ if (isset($this->path)) {
305
+ // store the old name to delete after the update
306
+ $this->temp = $this->path;
307
+ }
308
+ $this->path = null;
309
+ }
310
+
271
311
/**
272
312
* @ORM\PrePersist()
273
313
* @ORM\PreUpdate()
@@ -297,6 +337,14 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
297
337
$this->file->move($this->getUploadRootDir(), $this->path);
298
338
299
339
unset($this->file);
340
+
341
+ //check if we have an old image
342
+ if (isset($this->temp)) {
343
+ // delete the old image
344
+ unlink($this->getUploadRootDir().'/'.$this->temp);
345
+ // clear the temp image path
346
+ $this->temp = null;
347
+ }
300
348
}
301
349
302
350
/**
@@ -357,8 +405,22 @@ property, instead of the actual filename::
357
405
*/
358
406
class Document
359
407
{
360
- // a property used temporarily while deleting
361
- private $filenameForRemove;
408
+ private $temp;
409
+
410
+ /**
411
+ * Set file
412
+ *
413
+ * @param UploadedFile $file
414
+ */
415
+ public function setFile(UploadedFile $file)
416
+ {
417
+ $this->file = $file;
418
+ // check if we have an old image path
419
+ if (is_file($this->getAbsolutePath())) {
420
+ //store the old name to delete after the update
421
+ $this->temp = $this->getAbsolutePath();
422
+ }
423
+ }
362
424
363
425
/**
364
426
* @ORM\PrePersist()
@@ -390,23 +452,31 @@ property, instead of the actual filename::
390
452
);
391
453
392
454
unset($this->file);
455
+
456
+ //check if we have an old image
457
+ if (isset($this->temp)) {
458
+ // delete the old image
459
+ unlink($this->temp);
460
+ // clear the temp image path
461
+ $this->temp = null;
462
+ }
393
463
}
394
464
395
465
/**
396
466
* @ORM\PreRemove()
397
467
*/
398
468
public function storeFilenameForRemove()
399
469
{
400
- $this->filenameForRemove = $this->getAbsolutePath();
470
+ $this->temp = $this->getAbsolutePath();
401
471
}
402
472
403
473
/**
404
474
* @ORM\PostRemove()
405
475
*/
406
476
public function removeUpload()
407
477
{
408
- if ($this->filenameForRemove ) {
409
- unlink($this->filenameForRemove );
478
+ if ($this->temp ) {
479
+ unlink($this->temp );
410
480
}
411
481
}
412
482
0 commit comments