Generate zip file for download

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
emmbec
Forum Contributor
Posts: 112
Joined: Thu Sep 21, 2006 12:19 pm
Location: Queretaro, Mexico

Generate zip file for download

Post 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!
madan koshti
Forum Commoner
Posts: 50
Joined: Fri Jun 06, 2008 4:25 am

Re: Generate zip file for download

Post 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");
?>
Post Reply