Jump to content

Creating Zip Files using PHP


areeb
 Share

Recommended Posts

I wanted to ask whether there is any PHP script which can actually create and extract Zip files, once they are uploaded to the server.Well, Extraction of Zip files is possible using cPanel.The important thing is the creation of any compressed file e.g. Zip or Tar etc.Lets say, I have a folder in my site abc.I want such a script, which when executed, gives me abc.zip so that I can easily download it, instead of using FTP :)

Link to comment
Share on other sites

First you have to download PHPZip. Then do the following

<?PHP    set_time_limit(3000); //for big archives    require("../ss_zip.class.php");     // new empty archive with compression level 6    $zip= new ss_zip('',6);    //add file from disc and store to the archive under its own name and path    $zip->add_file('testdata/folder1/file1.html');    //add file from disc and store to the archive under our/path/ourfile2.html    $zip->add_file('testdata/folder1/file1.html','our/path/ourfile2.html');    //add file generated by the script and store it as testphp/thefile.html    $myfiledata="This content was generated by PHP script and stored to ZIP as a file";    $zip->add_data('testphp/thefile.html', $myfiledata);    //Saving the archive to server under name 'myzip1.zip'    $zip->save('myzip1.zip');?>

There are more functions here.

Link to comment
Share on other sites

You can also use PclZip (they list PHPZip as one of their projects, I'm not sure what that's about):http://www.phpconcept.net/pclzip/index.en.phpAnd here's the documentation:http://www.phpconcept.net/pclzip/man/en/in...907b0d831a8b30bThis code extracts a zip file, and adds each of the files (images, in this case) to a database:edit: it also deletes the extracted files once finished.

<?phprequire_once("pclzip.lib.php");//...$zipfile = new PclZip($filename);$zipfile->extract(PCLZIP_OPT_PATH, "zip_temp/", PCLZIP_OPT_SET_CHMOD, 0777, PCLZIP_OPT_REMOVE_ALL_PATH);$path = $temp_save . "zip_temp";	$dir_handle = opendir($path);	$path .= "/";while (false !== ($file = readdir($dir_handle))){	$valid = false;  if ($file != "." && $file != ".." && !is_dir($path . $file))  {    $components = explode(".", $file);    switch (strtolower($components[count($components) - 1]))    {      case 'gif':      case 'jpg':      case 'jpeg':      case 'wbmp':      case 'png':        $valid = true;        break;    }  }  if ($valid)  {    $data = fread(fopen($path . $file, "rb"), filesize($path . $file));    //add_image_to_db($path . $file, $data, $galID, $file);    $nr++;  }  if ($file != "." && $file != "..")  {  	chmod($path . $file, 0777);  	if (is_dir($path . $file))      rmdir($path . $file);    else      unlink($path . $file);  }}closedir($dir_handle);?>

Link to comment
Share on other sites

  • 2 weeks later...
You can also use PclZip (they list PHPZip as one of their projects, I'm not sure what that's about):http://www.phpconcept.net/pclzip/index.en.phpAnd here's the documentation:http://www.phpconcept.net/pclzip/man/en/in...907b0d831a8b30bThis code extracts a zip file, and adds each of the files (images, in this case) to a database:edit: it also deletes the extracted files once finished.

Thanks , I have used that PHPZip before but it was giving errors.Now I will try PCLZip.The code which u have given is for extracting files from a Zip Archives.What I am asking is to create a Zip File.Lets say I have a Folder named "data". When the Scripts runs, it should ZIP those files into a file and then offer me a link to download this archive :)While going through the Documentation, I have known this will be good >>
 <?php  include_once('pclzip.lib.php');  $archive = new PclZip('file.zip');  $v_list = $archive->create('file.txt,data/text.txt,folder');  if ($v_list == 0) {    die("Error : ".$archive->errorInfo(true));  }?>

Now this will create an archive named file.zip with the two files and one folder in it .... Right ??Now what should I do so that this file can be downloaded ??? :)Thanks

Link to comment
Share on other sites

If you want one script to generate and send the zip file, then you would follow the normal procedure for creating a file on disk. Once the zip is saved on disk, you can open the file using file_get_contents to save the file data as a string, and then use header to send headers for the filename, content type, size, etc.

header('Content-type: application/zip');header('Content-Disposition: attachment; filename="file.zip"');

Then you just send the file contents itself (echo the result of file_get_contents). This will cause the browser to download the file as a zip. Then to finish up you can delete the file later on.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...