Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> PHP

Nén thư mục thành file ZIP trong PHP

Sử dụng lớp ZipArchive (có sẵn từ PHP 5.3). Trên Windows, bật extension php_zip.dll trong php.ini.

Ví dụ

<?php
$sourceDir = "thu_muc_nguon/";
$zipFile = "archive.zip";

$zip = new ZipArchive;
if ($zip->open($zipFile, ZipArchive::CREATE) === TRUE) {
    $dir = opendir($sourceDir);
    while ($file = readdir($dir)) {
        if (is_file($sourceDir . $file)) {
            $zip->addFile($sourceDir . $file, $file);
        }
    }
    $zip->close();
    echo "Đã tạo $zipFile thành công";
} else {
    echo "Không thể tạo file ZIP";
}
?>