PHP Classes

Problem with Create Zip File

Recommend this page to a friend!

      Create ZIP File  >  All threads  >  Problem with Create Zip File  >  (Un) Subscribe thread alerts  
Subject:Problem with Create Zip File
Summary:Memory size problem with with Create Zip File
Date:2007-11-30 14:58:39
Update:2009-11-12 01:06:56

  1. Problem with Create Zip File   Reply   Report abuse  
Sudipto - 2007-11-30 14:58:39
I am using PHP Version - 5.2.4 and getting the following error when creating a zip file using the Create Zip File package.

The error i am getting is -

Fatal error: Allowed memory size of 50331648 bytes exhausted (tried to allocate 18853848 bytes).

Please let me know how to resolve this error.

  2. Re: Problem with Create Zip File   Reply   Report abuse  
Er. Rochak Chauhan - 2007-12-02 16:52:42 - In reply to message 1 from Sudipto
Hi check your phpinfo and look for "memory_limit"
The issue is with it. Apparently the zip file you are trying to create is over this size limit.

The solution is simple ! increase the memory limit using

ini_set("memory_limit", "2048M");

2048M depends on the desired zip file and on your server.
Good luck !

  3. Re: Problem with Create Zip File   Reply   Report abuse  
Attila Wind - 2009-08-11 09:20:33 - In reply to message 2 from Er. Rochak Chauhan

Good work!
But I noticed it needs a lot of memory to generate the .zip:
- growing .zip stream is kept in memory during the process
- adding a new file requires filesize*4 bytes additional free memory

I tried to decrease this.
Check this out:


  4. Re: Problem with Create Zip File   Reply   Report abuse  
Anon E Mouse - 2009-11-12 01:06:56 - In reply to message 3 from Attila Wind
That is brilliant Attila, thank you!

It took me a little while to figure out how to use it but it works perfectly, solves the memory problem you are a legend.

Here is an example page others can use to modify, to use Attila's updated version of this great class.

Thanks to Rochak too, for creating the class to begin with.

Example using Attila's version of the class:



include "conn_doc.php";


while(list($key,$value)= each($_GET)){
$$key = $value; // this line does the actual creation and assignment of variables

echo('<font face="Verdana, Arial, Helvetica, sans-serif" color="#000066" size="3"> Zipping your files now, please be patient....<BR>');

$directoryToZip="./"; // This will zip file(s) in this present working directory
//$outputDir="/"; // The desired output directory. NOTE: leaving it that way made some weird msg unzipping
$outputDir="/yourfiles/"; // The desired output directory.

$createZipFile=new CreateZipFile;

$fd=fopen($zipName, "wb");


$sql = "SELECT * from Job WHERE JobID=".$JobID." ORDER BY IncludeInList DESC, Priority";
$ri = mysql_query($sql);
for ($count = 1; $row = mysql_fetch_row ($ri); ++$count)
If ($Movie){
// Code to Zip a single file
$createZipFile->addFile($directoryToZip.$fileToZip, $outputDir.$fileToZip);
echo("adding ".$fileToZip."<BR>");
} // end If Movie

} //finish the Job recordset For Loop

mysql_close ();


echo("Successfully created file ready for downloading");

echo('<BR><BR><A HREF="'.$zipName.'"><font size=+2><B>Click here to Download the zip file</B></font></A>');