title: How to zip a file using symfony
body: <p>I uploaded a file and I want to zip it and then encode it in base64. The upload is going well. But when it comes to zipping, I encounter errors.
  Here is my controller</p>
  <p>FileController.php</p>
  <pre><code>&lt;?php
  
  namespace App\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  use Symfony\Component\Routing\Annotation\Route;
  use App\Entity\Upload;
  use App\Form\UploadType;
  use Doctrine\Common\Persistence\ObjectManager;
  use Doctrine\ORM\EntityManagerInterface;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\File\Exception\FileException;
  use Symfony\Component\HttpFoundation\File\UploadedFile;
  use Symfony\Component\String\Slugger\SluggerInterface;
  
  class FileController extends AbstractController
  {
      /**
       * @Route(&quot;/home&quot;, name=&quot;accueil&quot;)
       */
      public function index(Request $request, EntityManagerInterface $manager, SluggerInterface $slugger)
      {
  
          $upload = new Upload();
  
          $form = $this-&gt;createForm(UploadType::class, $upload);
  
          $form-&gt;handleRequest($request);
  
          if($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
              $manager-&gt;persist($upload);
              $manager-&gt;flush();
  
  
              $uploadedFile = $form-&gt;get('document')-&gt;getData();
  
              $originalFilename = pathinfo($uploadedFile-&gt;getClientOriginalName(), PATHINFO_FILENAME);
              $safeFilename = $slugger-&gt;slug($originalFilename);
              $newFilename = $safeFilename.'-'.uniqid().'.'.$uploadedFile-&gt;guessExtension();
              
              
              try {
                  $uploadedFile-&gt;move(
                      $this-&gt;getParameter('upload_directory'),
                      $newFilename
                  );
              } catch (FileException $e) {
                  // ... handle exception if something happens during file upload
              }
  
              $zip = new \ZipArchive();
              if ($zip-&gt;open('doc1.zip', ZipArchive::CREATE) == TRUE)
              {
                  // Add files to the zip file
                  $zip-&gt;addFile($uploadedFile);
               
                  // All files are added, so close the zip file.
                  $zip-&gt;close();
              }
              // Get the file and convert into string 
              $zip_f = file_get_contents('doc1.zip'); 
                    
              // Encode the zip file string data into base64 
              $data = base64_encode($zip_f);  
                  
              // Display the output 
              echo $data; 
  
  
              $upload-&gt;setDocument($newFilename);
              return $this-&gt;redirectToRoute('accueil');
  
          }
  
          return $this-&gt;render('file/index.html.twig', array(
              'form' =&gt; $form-&gt;createView(),
          ));
      }
  }
  
  </code></pre>
  <p>This is the message that i recieve when i run it:</p>
  <p><em>Attempted to load class &quot;ZipArchive&quot; from namespace &quot;App\Controller&quot;.
  Did you forget a &quot;use&quot; statement for another namespace?</em></p>