CodeIgniter Forums
Zip library "Allowed memory size" - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Zip library "Allowed memory size" (/showthread.php?tid=75264)



Zip library "Allowed memory size" - ZoeF - 01-19-2020

Hey all

I seem to be a hitting a snag in my program. I have a image gallery that has the function to download the images. When people are downloading 5-15 images there is no issue what so ever. But as soon as the try 50+ (or something close) I get a error. "Fatal error: Allowed memory size of 134217728 bytes exhausted". It links back to the zip library line 410.

I looked online but the only things I found that where a so called solution was to change the php.ini file and increase the limitations. The limitations are curently set to 128 MB and as we are using a hosting service that does not allow changes in these files I am at a loss IĀ suppose.

Is there any solution or diffrent thing I can try to get this to work? Maybe a function to split the download up in bite size chunks? Or something that takes it's time and does not eat up memory? Any idea will be appreciated.

I tried the "ini_set('memory_limit','1G');". But when I do that I recieve "Fatal error: Maximum execution time of 30 seconds exceeded". After I added the time limit and got the memory limit error again.

Just for a note the biggest folder of images at this time is around 680MB. But in the future it might go around 2-3 GB


RE: Zip library "Allowed memory size" - mjamilasfihani - 01-19-2020

Maybe you give limitation of download, ex 3 images every 10 minutes or whatever then you just need to tell the user with notification


RE: Zip library "Allowed memory size" - ZoeF - 01-20-2020

That is not an option. I should have mentioned it is a personal gallery of a user. And if they want to download there images they should be able to.


RE: Zip library "Allowed memory size" - futurewebs - 01-20-2020

just create your own function to add them to a zip file one by one rather than having CI do the whole thing in one pass

https://www.php.net/manual/en/class.ziparchive.php

not tested the following but you get the idea. This is what i did to get around the same issue

Code:
$zip = new ZipArchive();
$path_to_zip_file = '/home/path/to/file/my_zip_file.zip';
$array_of_files_to_save = array('/home/files/file1.jpg','/home/files/file3.jpg','/home/files/file3.jpg'); // create an array of files to save

// if zip does not exist, create it, otherwise open and add to it
$zip->open($path_to_zip_file , ZipArchive::CREATE):

// loop through and add each file
foreach($array_of_files_to_save AS $key => $value):
$zip->addFile($value)
endforeach;

// close the zip file
$zip->close();



RE: Zip library "Allowed memory size" - InsiteFX - 01-20-2020

Fixing PHP Fatal Error: Allowed Memory Size Exhausted


RE: Zip library "Allowed memory size" - ZoeF - 01-20-2020

@InsiteFX I appreciate any solutions that might be provided. But if you would have taken the time of reading my post in it's entirety you would have noticed I tried these solutions and also explained why I disliked them.


RE: Zip library "Allowed memory size" - jreklund - 01-20-2020

You can either do it like @futurewebs said. Or you will need to execute and compress those file outside of PHP with exec().
@futurewebs will be executed and will suffer the browser, server (apache/nginx) and/or PHP timeout. If you execute it outside PHP it will run on the server. But you will then need to look for said file at X interval (with Ajax) and see if it's done*.

* Should be last timestamp, haven't made a function like that in a while. Or you need to log tar/zip to a file and look for the "done" message there.


RE: Zip library "Allowed memory size" - ZoeF - 01-21-2020

mmmm the exec function sounds interesting. It is however something i never done before so i will need to experiment with it.


RE: Zip library "Allowed memory size" - InsiteFX - 01-21-2020

Here is another solution that might help you out.

Streamed File Zipping and Downloading in PHP


RE: Zip library "Allowed memory size" - futurewebs - 01-22-2020

In my test, it worked well as long as you are not trying to create a huge zip. In that case, yes, the browser is going to jam while its waiting for it to complete. You could create a table where you add the request as a job and then check with a chron job every 5 mins or so and process the requests or as mentioned do it using "exec". not all servers have that enabled though so you would need to check that first.