Welcome Guest, Not a member yet? Register   Sign In
Memory issues with ZIP Library
#1

[eluser]Xenon Design[/eluser]
Ive made a function to download some user selected image files and zip them and offer for download. Now my issue is that I am getting a PHP error.

This is the error:
Quote:Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 473365 bytes)

The line in which it relates to is 171 on libraries/Zip.php which is as follows:
Code:
$newoffset = strlen(implode("", $this->zipdata));

The function in my code that does all of this:
Code:
$rooturl = $this->config->item("base_url");
$this->load->library("zip");
$items = split(",", $this->input->post("items"));
$this->zip->clear_data();
$files = array();

$type = $this->uri->segment(4);

//var_dump($items);

foreach($items as $item){
    if($item != "null"){
        $data = $this->gallery->get_photo_data(0, $item);
        $files['photo_'.$data['photoid'].'.jpg'] = file_get_contents($rooturl."static/gallerybatch/".$data['batchid']."/".$data['filename'].".jpg");
    }
}

$this->zip->add_data($files);

$this->zip->download("images.zip");

There are about 23 images it is trying to download with each photo being around 300KB-600KB. Of course downloading only 5 or so images does work but the clients using this will need the whole lot when needed.

Now im pretty sure it has to do with some PHP or Apache settings some where to memory limits but buggered if i know where they are.

If anyone can help me that would be greatly appreciated. If you want to know more info on the issue then let me know and ill give what you need.
#2

[eluser]Derek Allard[/eluser]
We've got a decent summary of the issue up in the ExpressionEngine knowledgebase if you want to read, but the underlying issue is that your server is trying to do too much at once. One way around this would be to break it into smaller chunks. Try also turning your log_threshold (in config) up to errors only, or none, and see if that helps.
#3

[eluser]Xenon Design[/eluser]
Thanks Derek! I'll contact the host.
#4

[eluser]tonanbarbarian[/eluser]
your issues to put it simply is memory usage.
PHP can limit the amount of available memory on some systems.
By default PHP 4 only allowed 8M of memory but I believe this has increased in PHP 5 and 6 to 20M? but I could be wrong.
Your fatal error message indicates that your system is allowed 32M of memory

By trying to zip 23 files 300-600K in size you are zipping between 7 and 14M of data.
Since you are using PHP to do this it is loading all of that into memory, plus having to manipulate the data in memory to compress it.

Bascially you are using up all of the 32M of memory to zip these files.

My suggestion would be to try to get PHP to zip the files from the command line.
While I could give you a couple of ways of doing this it all depends on your server setup (hopefully not safemode because I do not think it will be possible), or as Derek suggested look at creating multiple zips files with maybe no more than 5 images per file.
#5

[eluser]helmutbjorg[/eluser]
Hey guys... how big a zip file can you make with the zip library? I am hitting the memory problem too and have bumped all my settings etc to no avail. Just wondering what sizes others have got it working for...




Theme © iAndrew 2016 - Forum software by © MyBB