https://www.icyphoenix.com/viewtopic.php?f=33&t=7868 ----------------------------------- Mighty Gorgon Sat 04 Jun, 2011 12:24 Batch ZIP Folders Using PHP ----------------------------------- I wanted to share this you this small snippet of code which I use to compress my styles before uploading here and submitting on phpbb.com for approval. Since I have designed several styles over the year, I get a bit annoyed when I need to pack up those styles for a new release... that's where PHP comes to help me. A quick example could help in understanding: [list][*]I have several styles in one folder: [list][*]black_pearl [*]frozen_phoenix [*]mg_autumn [*]mg_xmas [*]milky_way [*]milky_way_red [*]rainbow_pearl[/list] [*]I need to create a zip file for each of the above folders in a format like this: [list][*]black_pearl_1_6_4_20110603.zip [*]frozen_phoenix_1_6_4_20110603.zip [*]mg_autumn_1_6_4_20110603.zip [*]mg_xmas_1_6_4_20110603.zip [*]milky_way_1_6_4_20110603.zip [*]milky_way_red_1_6_4_20110603.zip [*]rainbow_pearl_1_6_4_20110603.zip[/list] [*]To do the job I just launch a PHP script[/list] Here are the basic instructions: [list=1][*]To be able to use this script you need [b]pclzip.lib.php[/b] library which you will find in Icy Phoenix package or here: [url=http://www.phpconcept.net/pclzip/pclzip-downloads]PCLZIP [*]Create a folder where you will put all subfolders to be zipped in different ZIP files, suppose you call this folder [b]backup[/b]. [*]Put in the folder you just created [b]pclzip.lib.php[/b] file [*]Put in the folder you just created all the folders you want to be zipped into separate files [*]Put this file ([b]make_zip.php[/b]) in that folder (highlighted in red you will find the parts you may want to change): [codeblock filename="make_zip.php"]Working on ' . $source_array[$i] . '
'); // Kill BAK echo('Deleting bak files'); flush(); $bak_files = array(); $bak_files = list_files_type($dest_path, 'bak'); for ($j = 0; $j < count($bak_files); $j++) { @unlink($bak_files[$j]); } echo(' ... DONE!!!
'); flush(); // Create ZIP package $zip_filename = 'phpbb3_' . $source_array[$i] . $style_version . '.zip'; echo('Creating ' . $zip_filename . ''); flush(); if (file_exists($zip_filename)) { @unlink($zip_filename); } $all_files_list = array(); $all_files_list = list_files($dest_path); if (isset($zip)) { unset($zip); } $zip = new PclZip($zip_filename); @chmod($zip_filename, 0777); $zip->add($all_files_list, $p_add_dir = '', $p_remove_dir = $source_array[$i]); echo(' ... DONE!!!
'); flush(); } output_footer(); exit; function output_header($page_title = 'Files List') { echo('' . "\n"); echo('' . "\n"); echo('' . $page_title . '' . "\n"); echo('' . "\n"); flush(); return true; } function output_footer() { echo('' . "\n" . ''); flush(); return true; } function list_files($source_folder) { $directory = @opendir($source_folder); $files_list = array(); while (@$file = readdir($directory)) { $full_path_file = $source_folder . '/' . $file; if (!in_array($file, array('.', '..'))) { if (is_dir($source_folder . '/' . $file)) { $files_list = array_merge($files_list, list_files($full_path_file)); } else { $files_list[] = str_replace('//', '/', ($full_path_file)); } } } @closedir($directory); sort($files_list); return $files_list; } function list_files_type($source_folder, $extension) { $directory = @opendir($source_folder); $files_list = array(); while (@$file = readdir($directory)) { $full_path_file = $source_folder . '/' . $file; if (!in_array($file, array('.', '..'))) { if (is_dir($source_folder . '/' . $file)) { $files_list = array_merge($files_list, list_files_type($full_path_file, $extension)); } else { $file_details = get_file_details($full_path_file); if ($file_details['ext'] == $extension) { $files_list[] = str_replace('//', '/', ($full_path_file)); } } } } @closedir($directory); sort($files_list); return $files_list; } function get_file_details($file_name) { $file_details = array(); $file_tmp = str_replace ('http://', '', $file_name); $file_path[] = array(); $file_path = explode('/', $file_tmp); $file_details['name_full'] = $file_path[count($file_path) - 1]; $file_part = explode('.', strtolower($file_details['name_full'])); $file_details['ext'] = $file_part[count($file_part) - 1]; $file_details['name'] = substr($file_details['name_full'], 0, strlen($file_details['name_full']) - strlen($file_details['ext']) - 1); return $file_details; } ?>[/codeblock] [*]You just need to make sure you edit this portion of code including a version number (if you want, otherwise you can leave that empty!) and listing all the folders you want to be zipped... timestamp will be added automatically: [codeblock]$style_version = '_1_6_4'; $date_suffix = '_' . date('Ymd'); $source_array = array('black_pearl', 'frozen_phoenix', 'mg_autumn', 'mg_xmas', 'milky_way', 'milky_way_red', 'rainbow_pearl');[/codeblock] [*]If you did everything correctly, by launching [b]make_zip.php[/b] all ZIP files will be created for the listed folders. [*]Remember to CHMOD 0777 the [b]backup[/b] folder (the folder where you put all the folders and make_zip.php).[/list] I hope you will find this snippet and quick tutorial useful.