@@ -55,23 +55,29 @@ First, create a simple Doctrine Entity class to work with::
55
55
56
56
public function getAbsolutePath()
57
57
{
58
- return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
58
+ return null === $this->path
59
+ ? null
60
+ : $this->getUploadRootDir().'/'.$this->path;
59
61
}
60
62
61
63
public function getWebPath()
62
64
{
63
- return null === $this->path ? null : $this->getUploadDir().'/'.$this->path;
65
+ return null === $this->path
66
+ ? null
67
+ : $this->getUploadDir().'/'.$this->path;
64
68
}
65
69
66
70
protected function getUploadRootDir()
67
71
{
68
- // the absolute directory path where uploaded documents should be saved
72
+ // the absolute directory path where uploaded
73
+ // documents should be saved
69
74
return __DIR__.'/../../../../web/'.$this->getUploadDir();
70
75
}
71
76
72
77
protected function getUploadDir()
73
78
{
74
- // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
79
+ // get rid of the __DIR__ so it doesn't screw up
80
+ // when displaying uploaded doc/image in the view.
75
81
return 'uploads/documents';
76
82
}
77
83
}
@@ -213,8 +219,12 @@ object, which is what's returned after a ``file`` field is submitted::
213
219
// use the original file name here but you should
214
220
// sanitize it at least to avoid any security issues
215
221
216
- // move takes the target directory and then the target filename to move to
217
- $this->file->move($this->getUploadRootDir(), $this->file->getClientOriginalName());
222
+ // move takes the target directory and then the
223
+ // target filename to move to
224
+ $this->file->move(
225
+ $this->getUploadRootDir(),
226
+ $this->file->getClientOriginalName()
227
+ );
218
228
219
229
// set the path property to the filename where you've saved the file
220
230
$this->path = $this->file->getClientOriginalName();
@@ -266,7 +276,8 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
266
276
{
267
277
if (null !== $this->file) {
268
278
// do whatever you want to generate a unique name
269
- $this->path = sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
279
+ $filename = sha1(uniqid(mt_rand(), true));
280
+ $this->path = $filename.'.'.$this->file->guessExtension();
270
281
}
271
282
}
272
283
@@ -373,7 +384,10 @@ property, instead of the actual filename::
373
384
// you must throw an exception here if the file cannot be moved
374
385
// so that the entity is not persisted to the database
375
386
// which the UploadedFile move() method does
376
- $this->file->move($this->getUploadRootDir(), $this->id.'.'.$this->file->guessExtension());
387
+ $this->file->move(
388
+ $this->getUploadRootDir(),
389
+ $this->id.'.'.$this->file->guessExtension()
390
+ );
377
391
378
392
unset($this->file);
379
393
}
@@ -398,7 +412,9 @@ property, instead of the actual filename::
398
412
399
413
public function getAbsolutePath()
400
414
{
401
- return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->id.'.'.$this->path;
415
+ return null === $this->path
416
+ ? null
417
+ : $this->getUploadRootDir().'/'.$this->id.'.'.$this->path;
402
418
}
403
419
}
404
420
0 commit comments