Error: Could not fetch http://thedevnet.com/wp-content/plugins/gd-star-rating/css/gdsr.css.php?t=0&s=a10i10m20k20c05r05%23121620243046%23121620243240%23s1pchristmas%23s1pcrystal%23s1pdarkness%23s1poxygen%23s1goxygen_gif%23s1pplain%23s1ppumpkin%23s1psoft%23s1pstarrating%23s1pstarscape%23t1pclassical%23t1pstarrating%23t1gstarrating_gif&ver=1.7.2 for caching. You might need to exclude this file in WP Minify options. The Developer Network – Easily Zip Your Files Using PHP

Easily Zip Your Files Using PHP

Sending multiple files to a user at once is as old as the internet is. The most commonly recognized format for compressing your files would be the zip format. By zipping your data you can make it use less resources to transmit to the user. It’s also a good way to clean up clutter. PHP has a very simple way of doing this.

Snippet:

  1. <?php
  2. // ZIP a file with PHP
  3. // http://thedevnet.com/php/snippets/zip-files-with-php/
  4.  
  5. /* creates a compressed zip file */
  6. function create_zip($files = array(),$destination = '',$overwrite = false) {
  7.  //if the zip file already exists and overwrite is false, return false
  8.  if(file_exists($destination) && !$overwrite) { return false; }
  9.  //vars
  10.  $valid_files = array();
  11.  //if files were passed in…
  12.  if(is_array($files)) {
  13.   //cycle through each file
  14.   foreach($files as $file) {
  15.    //make sure the file exists
  16.    if(file_exists($file)) {
  17.     $valid_files[] = $file;
  18.    }
  19.   }
  20.  }
  21.  //if we have good files…
  22.  if(count($valid_files)) {
  23.   //create the archive
  24.   $zip = new ZipArchive();
  25.   if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
  26.    return false;
  27.   }
  28.   //add the files
  29.   foreach($valid_files as $file) {
  30.    $zip->addFile($file,$file);
  31.   }
  32.   //debug
  33.   //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
  34.  
  35.   //close the zip — done!
  36.   $zip->close();
  37.   //check to make sure the file exists
  38.   return file_exists($destination);
  39.  }
  40.  else {
  41.   return false;
  42.  }
  43. }
  44. ?>

Example Usage:

  1. <?php
  2. // http://thedevnet.com/php/snippets/zip-files-with-php/
  3. $files=array('file1.jpg', 'file2.jpg', 'file3.gif');
  4. create_zip($files, 'myzipfile.zip', true);
  5. ?>
GD Star Rating
loading...

Originally posted 2009-10-23 10:17:51.

Popularity: 7%

Posted by natster   @   31 July 2010

Related Posts

Like this post? Share it!

RSS Digg Twitter StumbleUpon Delicious Technorati Facebook

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

You must be logged in to post a comment.

Previous Post
« AJAX Star Rating
Next Post
Simple PHP Database Class »
Powered by Wordpress   |   Lunated designed by ZenVerse