PHP Create Zip File

php create zip file

PHP Create Zip File. Something needs to be considered when creating a zip file using php is already loaded or not the zlib library by php runtime. With zlib enables you to transparently read or write ZIP compressed archives and the files inside them.

The bundled PHP 4 version requires » ZZIPlib, by Guido Draheim, version 0.10.6 or later
PHP 5.2.0 or later
This extension uses the functions of » zlib by Jean-loup Gailly and Mark Adler.

PHP Create Zip File Function

To use these functions, make sure that zlib have been loaded by php runtime. Use this trick to find zlib has been loaded by php, create new file php (Ex:phpinfo.php), fill the file with this(without quote): “<?php phpinfo();?>“. Put it on the web folder then open with mozilla fire fox.

If zlib have been loaded by php, Let’ start creating the Zip file.
* Create files 1.txt, 2.txt, 3.txt, 4.txt And Create makezip.php (in the same directory with file 1.txt).
Fill the makezip.php with this:

$zip = new ZipArchive();
$filename = "";
if ($zip-&gt;open($filename, ZIPARCHIVE::CREATE)!==TRUE) { exit("cannot open"); }
$zip-&gt;addFromString("1.txt", "time create zip ".time()." #1 This is a test string added as 1.txt.\n");
$zip-&gt;addFromString("2.txt" . time(), "#2 This is a test string added as 2.txt.\n");
$zip-&gt;addFile($thisdir . "3.txt","4.txt");
// file 3.txt while be renamed to 4.txt 
echo "numfiles: " . $zip-&gt;numFiles . "<br />";
echo "status:" . $zip-&gt;status . "<br />";
echo "Directory: ".getcwd()."<br />Filename: $filename"; $zip-&gt;close();

Upload files to your web directory, then open up makezip.php with your web browser. Look at your web directory, you will see the :D


Tinggalkan sebuah komentar

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

6 pemikiran di “PHP Create Zip File”

Mulai chat
Butuh Informasi?
Ya, Ada yang bisa saya bantu?