tags:

views:

46

answers:

2

Hi frnds can anyone tel me how to Create a Zip File Using PHP? Actually i am having 10 files with boxes,if i select more than one check box that files should get zip/rar and i am able to save that in some path..

Please can anyone help in this solution as i am new to php

+1  A: 
$zip_name = 'path/to/final.zip'; //the real path of your final zip file on your system
$zip = new ZipArchive;
$zip->open($zip_name, ZIPARCHIVE::CREATE);
foreach($files as $file)
{
    $zip->addFile($file);
}
$zip->close();

header('Content-type: application/zip');
header('Content-disposition: filename="' . $zip_name . '"');
header("Content-length: " . filesize($zip_name));
readfile($zip_name);
exit();
fabrik
path/to/final.zip which is this path? i want to install winzip in my system? actually i am using winrar..
edited my answer about the path. no, you don't need to install any third-party software. ZipArchive is PHP's built-in Zip extension. You can use it out of the box in PHP 5.2.0+
fabrik
zip is creating but whatever files i am selecting in checkbox that files not coming into zipfolder..wat is the problem?
Note: my code is an example only. These files are selectable via a HTML form? What's the name of your checkboxes?
fabrik
actually i am getting files in array one by one that name is$file_name //here i will get all the files from where i am selecting checkboxes
fine, and when you stepping through this array with foreach (like in my example) you cannot add actual files to your zip?
fabrik
i am adding actual files like($file_name as $file)then wat is the solution for adding seleting files in to that zip folder?
did you checked the files against file_exists()? if files exists you can add them with $zip->addFile($file); like in the example
fabrik
yes i checking file exisist as like below even its not working$path= "./upload/".$file_name;if(file_exists($path)){foreach($file_name as $file){ $zip->addFile($file_name);}
$file_name is an array? i don't think so.
fabrik
yes $file_name is an array
You can't concatenate a string and an array here: $path= "./upload/".$file_name; so $path cannot exists here: if(file_exists($path)) { ...
fabrik
+1  A: 
// This example creates a ZIP file archive test.zip and add the file /path/to/index.txt. as newname.txt. 
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFile('/path/to/index.txt', 'newname.txt');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
seengee
wht is that test.zipalso i dint get ('/path/to/index.txt','newname.txt');actually i want to create zip for word document