@@ -100,6 +100,7 @@ Finally, you need to update the code of the controller that handles the form::
100
100
namespace AppBundle\Controller;
101
101
102
102
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
103
+ use Symfony\Component\HttpFoundation\File\Exception\FileException;
103
104
use Symfony\Component\HttpFoundation\Request;
104
105
use Symfony\Component\Routing\Annotation\Route;
105
106
use AppBundle\Entity\Product;
@@ -123,11 +124,15 @@ Finally, you need to update the code of the controller that handles the form::
123
124
124
125
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
125
126
126
- // moves the file to the directory where brochures are stored
127
- $file->move(
128
- $this->getParameter('brochures_directory'),
129
- $fileName
130
- );
127
+ // Move the file to the directory where brochures are stored
128
+ try {
129
+ $file->move(
130
+ $this->getParameter('brochures_directory'),
131
+ $fileName
132
+ );
133
+ } catch (FileException $e) {
134
+ // ... handle exception if something happens during file upload
135
+ }
131
136
132
137
// updates the 'brochure' property to store the PDF file name
133
138
// instead of its contents
@@ -214,6 +219,7 @@ logic to a separate service::
214
219
// src/AppBundle/Service/FileUploader.php
215
220
namespace AppBundle\Service;
216
221
222
+ use Symfony\Component\HttpFoundation\File\Exception\FileException;
217
223
use Symfony\Component\HttpFoundation\File\UploadedFile;
218
224
219
225
class FileUploader
@@ -229,7 +235,11 @@ logic to a separate service::
229
235
{
230
236
$fileName = md5(uniqid()).'.'.$file->guessExtension();
231
237
232
- $file->move($this->getTargetDirectory(), $fileName);
238
+ try {
239
+ $file->move($this->getTargetDir(), $fileName);
240
+ } catch (FileException $e) {
241
+ // ... handle exception if something happens during file upload
242
+ }
233
243
234
244
return $fileName;
235
245
}
0 commit comments