Welcome to SEO - SEO News

SEO SEO News welcome authors from various web technologies to contribute articles and news. Articles should related to Search engine optimization, Web development and Webmaster resources only.


Display Your Website Pagerank in Your Homepage for Free
PageRank     PageRank    PageRank
Click Here to Get Free Code to Display PageRank


Find the following best SEO article


 »  Home  »  SEO Tutorials  »  Tutorial: Create a zip file from folders on the fly

Tutorial: Create a zip file from folders on the fly


For a future project I needed these days some easy to use zip or gzip class to create a zip file from files / folders inside a specified directory. A short search on Google has lead me to the Create ZIP File PHP class from Rochak Chauhan. I tested two other scripts before and must say that this script works great for single files if you add them manually.

To compress a whole directory with an unknown number of files into one zip file I created some class extension to get this job done.
I used some directory functions (opendir, readdir) to get all the files from the specified directory. Inside each directory there are files and “real directories” and directory locations. We need only the real directories. We test each value with “is_file”, “is_dir” and we filter off the directory directions (”.” and “..”). After the “file” is identified, the files content is added to the “addFile” object and for other directories the method “get_file_from_folder” is called again.
class createDirZip extends createZip {

  function get_files_from_folder($directory, $put_into) {
    if ($handle = opendir($directory)) {
      while (false !== ($file = readdir($handle))) {
        if (is_file($directory.$file)) {
          $fileContents = file_get_contents($directory.$file);
          $this->addFile($fileContents, $put_into.$file);
        } elseif ($file != ‘.’ and $file != ‘..’ and is_dir($directory.$file)) {
          $this->addDirectory($put_into.$file.‘/’);
          $this->get_files_from_folder($directory.$file.‘/’, $put_into.$file.‘/’);
        }
      }
    }
    closedir($handle);
  }
}

To get the files and sub directories from some directory we use this code:
$createZip = new createDirZip;
$createZip->addDirectory(‘themes/’);
$createZip->get_files_from_folder(‘blog/wp-content/themes/’, ‘themes/’);

We need create an object from the extension we have just created. Next we need only to define the base directory (which is used in the zip file) and the path to the directory. With this we are able to output the zip file:
$fileName = ‘tmp/archive.zip’;
$fd = fopen ($fileName, ‘wb’);
$out = fwrite ($fd, $createZip->getZippedfile());
fclose ($fd);

$createZip->forceDownload($fileName);
@unlink($fileName);

Via the method “getZippedfile” all content is added to the file (archive.zip) and the method “forceDownload” will send the created zip file to the browser.

I think this simple function is very helpful if you like to offer the public some dynamic files inside a directory or just to “zip - mail - backup” some directories from your web server using CRON.

http://www.web-development-blog.com

For more information visit SEO Tutorials category

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Verification:
Enter the security code shown below:
imgRegenerate Image