Objective of the blog:
The objective of this blog is to help you know how to fix the issue –“last file is not zipping”. If you are facing the same issue then this blog will help you. Example- If there are 4 text files then only 3 files are zipping.
Reason for this issue:
The zip_close($zip) function is not working due to which the file was unlinking before creating zip.
Solution:
You need to replace zip_close($zip) by $zip->close() to fix this issue.
Sample Code is given below:
$zip = new ZipArchive(); // zip 2 days older files foreach (glob($directory. '/*.txt') as $a_file) { if (filemtime($a_file) < time() - 172800) { //2 days older 172800 $filename_array = explode("/", $a_file); $key = sizeof($filename_array) - 1; $filename = $filename_array[$key]; $source_path = $directory.'/'; $zip_file = $source_path.str_replace('.txt','',$filename).".zip"; if ($zip->open($zip_file, ZipArchive::CREATE) !== TRUE) { exit("cannot open".$zip_file."\n"); } $zip->addFile($a_file,$filename); } } $zip->close(); // Replaced zip_close($zip) by $zip->close(); // Move 90 days olders in the Archive and remove the files which are older than 180 days foreach (glob($directory.'/*.zip') as $a_file) { // remove files which older than 180 days. if (filemtime($a_file) < time() - 15552000) { //180 days older 15552000 unlink($a_file); } }
Summary
In this blog, we have changed the zipping close function in order to fix the given issue.