@samara
To zip a file using Symfony, you can leverage the ZipArchive
class. Here's an example of how to do it:
- Install the zip PHP extension if not already installed:
sudo apt-get install php-zip
- Import the ZipArchive class at the top of your PHP class:
use ZipArchive;
- Create an instance of the ZipArchive class:
$zip = new ZipArchive();
- Open the zip archive and specify the path to the zip file:
$zip->open('/path/to/destination/archive.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
- Add the file you want to zip to the archive:
$zip->addFile('/path/to/source/file.txt', 'file.txt');
- You can also add multiple files by calling the addFile method multiple times.
- Close the zip archive:
$zip->close();
That's it! The file at the specified source path will be zipped and saved at the target destination path.