How to zip a file using symfony

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

FileController.php

<?php

namespace AppController;

use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentRoutingAnnotationRoute;
use AppEntityUpload;
use AppFormUploadType;
use DoctrineCommonPersistenceObjectManager;
use DoctrineORMEntityManagerInterface;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationFileExceptionFileException;
use SymfonyComponentHttpFoundationFileUploadedFile;
use SymfonyComponentStringSluggerSluggerInterface;

class FileController extends AbstractController
{
    /**
     * @Route("/home", name="accueil")
     */
    public function index(Request $request, EntityManagerInterface $manager, SluggerInterface $slugger)
    {

        $upload = new Upload();

        $form = $this->createForm(UploadType::class, $upload);

        $form->handleRequest($request);

        if($form->isSubmitted() && $form->isValid()) {
            $manager->persist($upload);
            $manager->flush();


            $uploadedFile = $form->get('document')->getData();

            $originalFilename = pathinfo($uploadedFile->getClientOriginalName(), PATHINFO_FILENAME);
            $safeFilename = $slugger->slug($originalFilename);
            $newFilename = $safeFilename.'-'.uniqid().'.'.$uploadedFile->guessExtension();
            
            
            try {
                $uploadedFile->move(
                    $this->getParameter('upload_directory'),
                    $newFilename
                );
            } catch (FileException $e) {
                // ... handle exception if something happens during file upload
            }

            $zip = new ZipArchive();
            if ($zip->open('doc1.zip', ZipArchive::CREATE) == TRUE)
            {
                // Add files to the zip file
                $zip->addFile($uploadedFile);
             
                // All files are added, so close the zip file.
                $zip->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->setDocument($newFilename);
            return $this->redirectToRoute('accueil');

        }

        return $this->render('file/index.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}

This is the message that i recieve when i run it:

Attempted to load class "ZipArchive" from namespace "AppController".
Did you forget a "use" statement for another namespace?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *