Function Code in zip format for php compressed files
- /* @ Creates a compressed zip file: function for compressing multiple files into one zip file
- * @ $ Files array type instance array ("1.jpg"," 2.jpg ");
- * @ Destination the path of the target file, for example, "c:/androidyue.zip"
- * @ $ Overwrite indicates whether to overwrite the same object as the target object.
- * @ Site http://bbs.it-home.org
- */
- Function create_zip ($ files = array (), $ destination = '', $ overwrite = false ){
- // If the zip file already exists and overwrite is false, return false
- // If the zip file already exists and is set to not overwrite, false is returned.
- If (file_exists ($ destination )&&! $ Overwrite) {return false ;}
- // Vars
- $ Valid_files = array ();
- // If files were passed in...
- // Obtain the real and valid file name
- If (is_array ($ files )){
- // Cycle through each file
- Foreach ($ files as $ file ){
- // Make sure the file exists
- If (file_exists ($ file )){
- $ Valid_files [] = $ file;
- }
- }
- }
- // If we have good files...
- // If a real and valid file exists
- If (count ($ valid_files )){
- // Create the archive
- $ Zip = new ZipArchive ();
- // Open the file and overwrite it if it already exists. otherwise, create
- If ($ zip-> open ($ destination, $ overwrite? ZIPARCHIVE: OVERWRITE: ZIPARCHIVE: CREATE )! = True ){
- Return false;
- }
- // Add the files
- // Add a file to the compressed file
- Foreach ($ valid_files as $ file ){
- $ Zip-> addFile ($ file, $ file );
- }
- // Debug
- // Echo The zip archive ins, $ zip-> numFiles, 'files with a status of ', $ zip-> status;
- // Close the zip -- done!
- // Close the file
- $ Zip-> close ();
- // Check to make sure the file exists
- // Check whether the file exists
- Return file_exists ($ destination );
- } Else {
- // If no valid file exists, false is returned.
- Return false;
- }
- }
- /****
- // Test the function
- $ Files = array ('temp. php', 'Test. php ');
- Create_zip ($ files, 'myzipfile.zip ', true );
- ****/
- ?>
|