Created
January 31, 2024 08:39
-
-
Save wichaksono/77a11ad9e712678b67a35149cd81b663 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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