@@ -100,6 +100,7 @@ Finally, you need to update the code of the controller that handles the form::
100100 namespace App\Controller;
101101
102102 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
103+ use Symfony\Component\HttpFoundation\File\Exception\FileException;
103104 use Symfony\Component\HttpFoundation\Request;
104105 use Symfony\Component\Routing\Annotation\Route;
105106 use App\Entity\Product;
@@ -123,11 +124,15 @@ Finally, you need to update the code of the controller that handles the form::
123124
124125 $fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
125126
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+ }
131136
132137 // updates the 'brochure' property to store the PDF file name
133138 // instead of its contents
@@ -219,6 +224,7 @@ logic to a separate service::
219224 // src/Service/FileUploader.php
220225 namespace App\Service;
221226
227+ use Symfony\Component\HttpFoundation\File\Exception\FileException;
222228 use Symfony\Component\HttpFoundation\File\UploadedFile;
223229
224230 class FileUploader
@@ -234,7 +240,11 @@ logic to a separate service::
234240 {
235241 $fileName = md5(uniqid()).'.'.$file->guessExtension();
236242
237- $file->move($this->getTargetDirectory(), $fileName);
243+ try {
244+ $file->move($this->getTargetDir(), $fileName);
245+ } catch (FileException $e) {
246+ // ... handle exception if something happens during file upload
247+ }
238248
239249 return $fileName;
240250 }
0 commit comments