Skip to content

Instantly share code, notes, and snippets.

@wichaksono
Created January 31, 2024 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wichaksono/77a11ad9e712678b67a35149cd81b663 to your computer and use it in GitHub Desktop.
Save wichaksono/77a11ad9e712678b67a35149cd81b663 to your computer and use it in GitHub Desktop.
<?php
$folderPath = 'gpuniversity'; // Ganti dengan path folder Anda
$zipFileName = 'gpuniversity.zip'; // Nama file ZIP yang akan dibuat
$zip = new ZipArchive();
// Membuka arsip ZIP untuk menulis
if ($zip->open($zipFileName, ZipArchive::CREATE) === TRUE) {
// Mendapatkan panjang string dari path root
$rootPathLength = strlen(realpath($folderPath)) + 1;
// Menambahkan file dari direktori ke arsip ZIP
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($folderPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
// Jangan memasukkan direktori
if (!$file->isDir()) {
$filePath = $file->getRealPath();
// Mendapatkan path relatif dengan menghapus bagian root path
$relativePath = substr($filePath, $rootPathLength);
// Menambahkan file ke arsip
$zip->addFile($filePath, $relativePath);
}
}
// Menutup arsip ZIP
$zip->close();
echo 'Arsip ZIP berhasil dibuat.';
} else {
echo 'Gagal membuat arsip ZIP.';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment