How can I compress specific files or compress specific folder or compress bulk files using php.Only you want to add zip class and pass parameters as you want.
This class can be used to create archives of compress files in ZIP format.You can compressed archive to a file instead of a variable when the archive size exceeds a given memory usage threshold.
Also you can add comments to the zip file,rename final zip file,get Archive/zip files size.
Compress specific file
setComment("Compress specific file using PHP.\nCreated on " . date('l jS \of F Y h:i:s A')."\nTutorial :- http://webexplorar.com/php-creating-zip-files/"); // add files - Syntax //$zip->addFile('file contents', $FileName); $zip->addFile(file_get_contents($fileDir . $fileName), $fileName, filectime($fileDir . $fileName)); // creating zip file $zip->sendZip("PHP_Compress_specific_file_example.zip"); ?>
Download Compress specific file using PHP Example (33 KB)
Compress specific folder with it’s all files
setComment("Compress specific folder using PHP.\nCreated on " . date('l jS \of F Y h:i:s A')."\nTutorial :- http://webexplorar.com/php-creating-zip-files/"); // add files inside a folder foreach (new DirectoryIterator($fileDirectory) as $file) { if($file->isDot()) continue; $zip->addFile( file_get_contents($fileDirectory.$file->getFilename(),$fileDirectory),$file->getFilename() ); } // creating zip file $zip->sendZip("PHP_Compress_specific_folder_example.zip"); ?>
Download Compress specific folder using PHP Example (2.35 MB)
Compress more files using PHP
setComment("Compress more files using PHP.\nCreated on " . date('l jS \of F Y h:i:s A')."\nTutorial :- http://webexplorar.com/php-creating-zip-files/"); // add files - Syntax //$zip->addFile('file contents', $FileName); $fileArr = explode(',',$fileName); for($i=0; $iaddFile( file_get_contents($fileDir.$fileArr[$i],$fileDir),$fileArr[$i]); } // creating zip file $zip->sendZip("PHP_Compress_more_files_example.zip"); ?>
Download Compress more files using PHP Example (1MB)