Reputation: 659
I am working with Files and Folders within CakePHP. Now everything works fine and in the way I want it to. However, when Zipping files, I get the following error message :
Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 240047685 bytes)
Now zipping smaller files, its fine! I have even done files in size of around 10MB without any issues, however zipping that are larger in size seem to have an issue.
Now I have added the following to my .htaccess file and made a php.ini file as I thought that might be the issue.
php_value upload_max_filesize 640000000M
php_value post_max_size 640000000M
php_value max_execution_time 30000000
php_value max_input_time 30000000
Until I found some posts pointing at the fact that PHP as a 4GB file limit. Well even if that is the case, why does my zip file not do this file (which is only about 245mb).
public function ZippingMyData() {
$UserStartPath = '/data-files/tmp/';
$MyFileData = $this->data['ZipData']; //this is the files selected from a form!
foreach($MyFileData as $DataKey => $DataValue) {
$files = array($UserStartPath.$DataValue);
$zipname = 'file.zip';
$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($files as $file) {
$path = $file;
if(file_exists($path)) {
$zip->addFromString(basename($path), file_get_contents($path));
} else {
echo"file does not exist";
}
} //End of foreach loop for $files
} //End of foreach for $myfiledata
$this->set('ZipName', $zip_name);
$this->set('ZipFiles', $MyFileData);
$zip->close();
copy($zip_name,$UserStartPath.$zip_name);
unlink($zip_name); //After copy, remove temp file.
$this->render('/Pages/download');
} //End of function
Any ideas of where I am going wrong? I will state that this is NOT my code, I found bits of it on others posts and changed it to fit my needs for my project!
All help most welcome...
Thanks
Glenn.
Upvotes: 1
Views: 889
Reputation: 420
I think that ZipArchive
loads your file in memory, so you have to increase the memory_limit
parameter in php.ini.
To avoid consuming all the memory of your server and drop performance, if your file is big, a better (but far from be the best) solution should be:
public function ZippingMyData() {
$UserStartPath = '/data-files/tmp/';
$MyFileData = $this->data['ZipData']; //this is the files selected from a form!
foreach($MyFileData as $DataKey => $DataValue) {
$files = array($UserStartPath.$DataValue);
$zip_name = time().".zip"; // Zip name
// Instead of a foreach you can put all the files in a single command:
// /usr/bin/zip $UserStartPath$zip_name $files[0] $files[1] and so on
foreach ($files as $file) {
$path = $file;
if(file_exists($path)) {
exec("/usr/bin/zip $UserStartPath$zip_name basename($path)");
} else {
echo"file does not exist";
}
} //End of foreach loop for $files
} //End of foreach for $myfiledata
$this->render('/Pages/download');
} //End of function
or similar (depends on your server). This solution has only two limits: disk space and zip limitations.
I apologize for the poor quality of my code and for any error.
Upvotes: 1