Edmonds Commerce Logo
  • home
    • blog
  • ecommerce
    • product catalogue
    • order processing
    • customer services
    • stock control
    • human resources
    • management information
  • development
    • oscommerce
    • php
    • mysql
    • open source
    • performance tuning
  • design
  • marketing
  • contact us
    • pricing

Edmonds Commerce Blog

Freelance PHP Ecommerce and SEO Developer in the UK

Latest Posts

CRELoaded Remove Google Ads -
ICECat Integration with osCommerce, Magento etc
Magento UK
PHP Cached Download Function

Most Popular Posts

PHP Email Attachment Function Freelance osCommerce UK Ultimate osCommerce Checkout - Fast and Friendly PHP : Dead Easy Excel Export

Archive for the 'zip' Category

PHP and Zip Files

Thursday, March 13th, 2008

There aren't many things you can't do with PHP. One of the things you can do is deal with zip files.

PHP has a built in class for dealing with zip files which allows you to create them, unpack them, add and delete things from them and generally use them within your scripts.

Recently on a spidering / product feed integration job that I have been working on I needed to grab a load of image zip files and unpack them all into a folder called 'images/' (surprisingly enough)

Here is how I did it:

PLAIN TEXT
PHP:
  1. function unpack_zips($directory, $destination = 'images/'){
  2.     $files = dir_list($directory);
  3.     //print_r($files);
  4.     $zips = array();
  5.     foreach($files as $k=>$file){
  6.         if(stristr($file, '.zip')){
  7.             $zips[] = $directory . $file;
  8.         }
  9.     }
  10.     print_r($zips);
  11.     $zip = new ZipArchive;
  12.     foreach($zips as $z){
  13.         if ($zip->open($z) === TRUE) {
  14.             $zip->extractTo($directory . $destination);
  15.             $zip->close();
  16.             //echo "<h3>$z OK</h3>";
  17.         } else {
  18.             echo"<h3 style=\"color: red;\">$z Failed</h3>";
  19.             bottom();
  20.         }   
  21.     }
  22. }
  23.  
  24. function dir_list($directory){
  25.     echo $directory;
  26.     if ($handle = opendir($directory)) {
  27.         while (false !== ($file = readdir($handle))) {
  28.             if($file != '.' && $file != '..'){
  29.                 $return[] = $file;
  30.             }
  31.         }
  32.         closedir($handle);
  33.         return $return;
  34.     }else{   
  35.         return false;
  36.     }
  37. }

There are two functions. The first function is the one that deals with the Zip files. The second function is called by the first function and simply lists all files within a specified directory.

These two functions combined allowed me to find all zip files in a particular folder and then unpack them all into a destination folder.

Related Resources

http://www.phpclasses.org/browse/package/945.html

http://www.phpit.net/article/creating-zip-tar-archives-dynamically-php/

Posted in php, zip | No Comments »

  • RSS Feed
  • Categories

    • apache
    • barcode
    • creloaded
    • curl
    • customer services
    • debugging
    • ecommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • link building
    • linux
    • magento
    • management
    • mod_rewrite
    • mysql
    • oscommerce
    • php
    • plesk
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • scraping
    • search engine optimisation
    • spidering
    • ubuntu
    • web design
    • web development
    • Windows
    • xampp
    • zip
  • Archives

    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    addons advanced adverts blackhat blocking css curl development directories find firefox google hosts file html javascript keywords links msn mysql myths operators oscommerce paid links paid placement performance php ppc reciprocal linking replace screen scraping security seo serp speed spider spidering tuning user friendly vista web web design web developer
  • Random Posts

    • Advanced PHP Debug Function
    • PHP Random Sleep Function with Flush
    • Regular Expression Test Tool
    • HOSTS file, Vista and Blocking Unwanted Adverts
    • Meta Title Tag and SEO
    • EAN13 Barcode Check Digit with PHP
    • File Comparison on Ubuntu
    • Online Backend System
    • PHP 301 Redirect Function with Headers Check and Javascript Fallback
    • Product Feed Integration and Scraping Products from Supplier Web Sites

Edmonds Commerce related questions? Send us a message or call us on 0844 357 0201.

Freelance PHP Web Design UK Commercial Web Design