Showing posts with label archive. Show all posts
Showing posts with label archive. Show all posts

Thursday, July 03, 2014

ใช้ php รวมแฟ้มใน directory เข้า .zip


ตั้งแต่ php 5.2 ขึ้นไปมีฟังก์ชัน ziparchive
ทำให้สามารถนำแฟ้มใน dir ที่เราดูแลใส่เข้าไปในแฟ้ม
.zip เพียงแฟ้มเดียวได้ และ download ออกมาได้เลย
จากการทดสอบแล้วไม่พบปัญหาใด
แต่ถ้าแฟ้มมีขนาดใหญ่ หรือจำนวนแฟ้มหลายร้อยแฟ้ม
น่าจะต้องปรับให้เลือกแฟ้มได้ ว่าจะนำแฟ้มใดเข้า .zip
เพราะถ้าทั้งหมดคงมีปัญหาในอนาคตอย่างแน่นอน

$zip = new ZipArchive();
if ($zip->open("x.zip", ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
}
$zip->addFromString(time() . "a.txt" , "#1 This is a test string added as a.txt.\n");
$zip->addFromString(time() . "b.txt" , "#2 This is a test string added as b.txt.\n");
$zip->addFile("c.php");
$zip->addFile("d.php");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
// http://php.net/manual/en/zip.examples.php
// testing on xampp 1.8.3
?>

Think different