Page 1 of 1

Generate zip file for download

Posted: Fri Jun 27, 2008 10:41 am
by emmbec
Hi everyone, I am working on an application to generate resumes of employees from a MySQL Database. Currently I generate the Word documents for the resumes (That is the format they asked me for) using COM libraries. I generate the CV's inside a folder in my web server but now I would like to zip them and put them available for download. I checked in the php.net site for reference (http://lt.php.net/manual/en/ref.zip.php), and I found the following code, which seems to do what I need:

Code: Select all

 
class MyZipArchive extends ZipArchive
{
    /**
     *
     * Adds a directory recursively.
     *
     * @param string $filename The path to the file to add.
     *
     * @param string $localname Local name inside ZIP archive.
     *
     */
    public function addDir($filename, $localname)
    {
        $this->addEmptyDir($localname);
        $iter = new RecursiveDirectoryIterator($filename);
 
        foreach ($iter as $fileinfo) {
            if (! $fileinfo->isFile() && !$fileinfo->isDir()) {
                continue;
            }
 
            $method = $fileinfo->isFile() ? 'addFile' : 'addDir';
            $this->$method($fileinfo->getPathname(), $localname . '/' .
                $fileinfo->getFilename());
        }
    }
}
 
The problem is, that I have never used classes in PHP :oops: how could I use that code, so that I can zip all of my resumes that are inside an specific folder?

Thanks!

Re: Generate zip file for download

Posted: Sat Jun 28, 2008 3:53 am
by madan koshti
1] make sure extension=php_zip.dll enabled
2] change code as per requirement
<?php
$archivename = "test.zip";
$zipobject = new ZipArchive();
$zipobject->open($archivename, ZIPARCHIVE::CREATE);
$zipobject->addFile("test1.php");
$zipobject->addFile("test.php");
$zipobject->close();
//echo "The new archive has been created";
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=test.zip");
?>