Friday, May 25, 2012

How to download and unzip a .ZIP file using php

Today I was working on a website which will be an affiliate member of another big company with already running a very good website ( i don't want to mention the name of that affiliate program because with this my idea is some how disclosed to the public and i don't want to do so :) ). In that affiliate program I have to download the feeds in .zip form and then importing that in my database. So I needed a script to automatically download, unzip and then import that data in my database. By using my experience and skills and also with the help of Google, I have successfully written a script for this purpose. In this post I am going to share the same,

First part of my script checks that php zip extension is loaded or not.
 if (!extension_loaded('zip')) {  
                dl('zip.so');  
           }  
After that, mention the path of download file and destination folder and rest of the script is as follows,
 $target_url = "http://www.example.com/test.zip";  
 $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';  
 $file_zip = "downloads/new.zip";  
 $file_txt = "downloads/".md5(time())."new.txt";  
 echo "<br>Starting<br>Target_url: $target_url";  
 echo "<br>Headers stripped out";  
 // make the cURL request to $target_url  
 $ch = curl_init();  
 $fp = fopen("$file_zip", "w");  
 curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);  
 curl_setopt($ch, CURLOPT_URL,$target_url);  
 curl_setopt($ch, CURLOPT_FAILONERROR, true);  
 curl_setopt($ch, CURLOPT_HEADER,0);  
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
 curl_setopt($ch, CURLOPT_AUTOREFERER, true);  
 curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);  
 curl_setopt($ch, CURLOPT_TIMEOUT, 10);  
 curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);   
 curl_setopt($ch, CURLOPT_FILE, $fp);  
 $page = curl_exec($ch);  
 if (!$page) {  
   echo "<br />cURL error number:" .curl_errno($ch);  
   echo "<br />cURL error:" . curl_error($ch);  
   exit;  
 }  
 curl_close($ch);  
 echo "<br>Downloaded file: $target_url";  
 echo "<br>Saved as file: $file_zip";  
 echo "<br>About to unzip ...";  
 // Un zip the file  
 $zip = new ZipArchive;  
   if (! $zip) {  
     echo "<br>Could not make ZipArchive object.";  
     exit;  
   }  
   if($zip->open("$file_zip") != "true") {  
       echo "<br>Could not open $file_zip";  
         }  
   $zip->extractTo("$file_txt");  
   $zip->close();  
 echo "<br>Unzipped file to: $file_txt<br><br>";  
If you like this script, so please post your comments and feedback. Your good feedback is the main desire of me to continue writing useful posts for my blog reader. Thanks

2 comments:

  1. Great, I´m going to test it.

    ReplyDelete
  2. Ok, but for me, it only worked after inserting

    curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).DIRECTORY_SEPARATOR.'c00kie.txt');

    as explained here http://stackoverflow.com/questions/12400747/curl-returning-blank

    best regards.

    ReplyDelete

Please feel free to post your comments. If anyone has a good article or good thing to share, just send me that with your name to asadmehmoodstar@gmail.com. and if anyone want so receive updates regarding my blog, he can subscribe to my weekly newsletter on "Subscribe to our mailing list" section.

Thanks