function init() {
ob_start();
// Ensure the zip directory exists
if (!file_exists($this->zipDir)) {
mkdir($this->zipDir, 0755, true);
}
// Create the ZIP file
$this->zip = new ZipArchive();
if ($this->zip->open($this->zipPath, ZipArchive::OVERWRITE) !== TRUE) {
if ($this->zip->open($this->zipPath, ZipArchive::CREATE) !== TRUE) {
throw new Exception("Cannot create zip file");
}
// ... (truncated)
âŠī¸ Returns
(bool) Success status
â ī¸ Throws
Exception: If ZIP creation fails
đ§ addFile
Add a file to the ZIP archive
function addFile($filePath, $filename) {
if (!file_exists($filePath)) {
return false;
}
$this->files[] = [$filePath => $filename];
return $this->zip->addFile($filePath, $filename);
}
âī¸ Parameters
$filePath(string) Path to the file to add
$zipPath(string) Path inside the ZIP where the file should be stored
âŠī¸ Returns
(bool) Success status
đ§ addCSV
Create a CSV file and add it to the ZIP
function addCSV($csvName, array $headers, array $data) {
$csvPath = $this->zipDir . '/' . $csvName;
$csvHandle = fopen($csvPath, 'w');
// Write headers
fputcsv($csvHandle, $headers);
// Write data
foreach ($data as $row) {
fputcsv($csvHandle, $row);
}
fclose($csvHandle);
// Add CSV to ZIP
$this->addFile($csvPath, $csvName);
// Mark file for cleanup
$this->filesToCleanup[] = $csvPath;
// ... (truncated)