PHP: Error (500) when creating a zip file with ZipArchive

I am using php ZipArchive to create a zip directory file. The code works fine for almost all directories except the two which are the largest (the largest directory currently contains 51 files with a total capacity of 175 MB). When I run the code for these directories, a temporary file ('filename.zip. [RandomLettersAndNumbers]', for example 'filename.zip.riaab4') will be created with a size of 67 108864 bytes, and the script will generate an internal server error (500).

What I have tried so far (most of them are still visible in the source):

  • Increase memory_limit
  • Increase max_execution_time
  • Check php error log: error log is empty
  • Try to find the exact line where the error occurs: the code runs until $zip_archive->close();

A source:

 //Temporary (debugging) START ini_set("log_errors", 1); ini_set("error_log", "php-error.log"); error_log("Hello errors."); ini_set('memory_limit','512M'); set_time_limit(180); //phpinfo(); //Temporary (debugging) END //Get selected directory // --> Code removed $result['directory'] = 'directory1'; //Create .zip file $zip_archive = new ZipArchive(); $ZIP = 'files/'.$result['directory'].'.zip'; if($zip_archive->open($ZIP, ZipArchive::CREATE | ZIPARCHIVE::OVERWRITE) !== TRUE) { echo 'Error! Error while creating ZIP Archive.'; die; } foreach(new DirectoryIterator('files/'.$result['directory']) as $fileInfo) { if($fileInfo->isDot()) continue; if(file_exists($fileInfo->getPath())) $zip_archive->addFile('files/'.$result['directory'].'/'.$fileInfo->getFilename(), $fileInfo->getFilename()); } //Temporary (debugging) START echo '<br><br><br>'; var_dump($zip_archive->close()); // ## The following lines are not executed!! ## echo '<br><br><br>'; var_dump($zip_archive->getStatusString()); echo '<br><br><br>'; //Temporary (debugging) END if($zip_archive->numFiles > 0 && $zip_archive->close() == true) $FILE = $ZIP; else { } 

Am I missing something? If there is anything else I can try, please let me know. Thanks.

EDIT: I have a more specific question: why do temporary zip files from affected directories have a file size of 67 108 864 bytes? Is this related to the maximum file size set by php / server, or can this be explained by the zip standard / ZipArchive?

+5
source share
1 answer

Try closing the ZIP archive and reopening it after every 5 or so files have been added to the archive. I assume that this solution solves the problem with memory limitations.

Thanks to Matthews for this decision.

0
source

Source: https://habr.com/ru/post/1240367/


All Articles